How can I upload multiple files to a model field?

后端 未结 3 1008
野的像风
野的像风 2020-12-08 12:05

I want to upload multiple files through a ModelForm,with all files to be assigned to a file field of the Model.I have gone through the docs and I saw an example

3条回答
  •  天命终不由人
    2020-12-08 12:31

    You have to create a separate model for the files and connect them with a foreign key:

    class Feed(models.Model):
        user=models.ForeignKey(User, on_delete=models.CASCADE, related_name='feeds')
        text=models.TextField(blank=False, max_length=500)
    
    
    class FeedFile(models.Model):
        file = models.FileField(upload_to="files/%Y/%m/%d")
        feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name='files')
    

    I hope this helps.

提交回复
热议问题