简体   繁体   English

如何在django管理模型中上传多个文件

[英]How to upload multiple file in django admin models

file = models.FileField(upload_to=settings.FILE_PATH)

For uploading a file in django models I used the above line. 为了在django模型中上传文件,我使用了上面的代码。 But For uploading multiple file through django admin model what should I do? 但是要通过django admin模型上传多个文件我该怎么办? I found this But this is for forms. 我找到了这个,但这是表格。 Can I use this for models? 我可以将它用于模特吗?

If you want to have multiple files for the same field you would have to write your own field and widget based on the form field you have found otherwise have a separate model for file with a foreign key to your main model and use ModelInline. 如果您想为同一个字段创建多个文件,则必须根据您找到的表单字段编写自己的字段和窗口小部件,否则将为具有外键的文件创建单独的模型,并使用ModelInline。

models.py models.py

class Page(models.Model):
    title = models.CharField(max_length=255)

class PageFile(models.Model):
    file = models.ImageField(upload_to=settings.FILE_PATH)
    page = models.ForeignKey('Page')

admin.py admin.py

 class PageFileInline(admin.TabularInline):
        model = PageFile

 class PageAdmin(admin.ModelAdmin):
        inlines = [PageFileInline,]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM