如何在django管理模式中上传多个文件 [英] How to upload multiple file in django admin models

查看:1143
本文介绍了如何在django管理模式中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

为了在django模型中上传文件,我使用了上面的一行。但是要通过django管理员模型上传多个文件,该怎么办?我发现这个但是这是为形式。我可以用这个模型吗?

For uploading a file in django models I used the above line. But For uploading multiple file through django admin model what should I do? I found this But this is for forms. Can I use this for models?

推荐答案

如果你想为同一个字段添加多个文件,你必须自己编写根据您发现的表单字段,字段和窗口小部件另有一个独立的文件模型,使用外部键到您的主模型并使用ModelInline。

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.

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

 class PageFileInline(admin.TabularInline):
        model = PageFile

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

这篇关于如何在django管理模式中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆