Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to create an array of files in a model in django?

I am creating a web app for cloud storage where user can create folders and upload files. I want to create a model for files like:

class upload(models.Model):
    files = # array of files which I don't know how to create
    title = models.CharField(max_length = 30)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

I will be thankful if anyone suggests me how to do so or tell me any other efficient way to upload files and use it.

like image 437
Hashir Sarwar Avatar asked Oct 22 '25 16:10

Hashir Sarwar


1 Answers

You need to create another model (e.g. UploadFragment) and bind it with a foreign key relation to your Upload model.

class Upload(models.Model):
    title = models.CharField(max_length = 30)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

class UploadFragment(models.Model):
    file = models.FileField()
    upload = models.ForeignKey(Upload, related_name="files", on_delete=models.CASCADE)
like image 71
dahrens Avatar answered Oct 25 '25 14:10

dahrens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!