【发布时间】:2019-08-27 19:41:31
【问题描述】:
我想在 Django 管理中上传多个文件,而不必放置多个 FileField 字段。用户可以以简单的方式管理文件;删除或更改每个上传的文件,但一次上传多个。
我认为可行的解决方案是使用多个文件字段,但问题是,我不知道用户将上传多少文件
def case_upload_location(instance, filename):
case_name = instance.name.lower().replace(" ", "-")
file_name = filename.lower().replace(" ", "-")
return "casos/{}/{}".format(case_name, file_name)
class Case(models.Model):
name = models.CharField(max_length=250)
observations = models.TextField(null = True, blank = True)
number_folder = models.CharField('Folder', max_length=250)
file1 = models.FileField('file 1', upload_to=case_upload_location, null = True, blank = True)
file2 = models.FileField('file 2', upload_to=case_upload_location, null = True, blank = True)
file3 = models.FileField('file 3', upload_to=case_upload_location, null = True, blank = True)
file4 = models.FileField('file 4', upload_to=case_upload_location, null = True, blank = True)
最终目标
要上传多个文件(用户需要一个一个删除或更改,但一次上传)。
【问题讨论】:
-
为什么不拥有一个单独的模型,它只包含一个文件字段,并且可以使用 FK 链接到
Case并且您可以通过管理员添加内联以添加文件
标签: python django django-models django-forms django-file-upload