【问题标题】:Django Inline model frontendDjango 内联模型前端
【发布时间】:2017-07-30 20:02:23
【问题描述】:

我的模型需要四个文件字段,所以我使用 Django 内联模型。我还需要为所有 thode 字段创建一个模型,以便用户可以填写表单,并且我正在使用基于类的视图 - CreateView。但是我不知道如何在我的模型表单中获取文件字段。

模型.py

class Product(models.Model)

name= models.Charfield(maxlength=50)
city= models.Charfield(maxlength=50)
state=model.Charfield(maxlength=50)
year=models.Integerfield(blank=True, null=True)

class ProductAttachment(models.Model)

attachment = models.ForeignKey(Product,    related_name='attachment')
appendix_a= models.Filefield(verbose_name='Appendix A')
appendix_b= models.Filedield(verbose_name='Appendix B')
Other = models.Filefield()

Admin.py

class ProductInline(admin.StackedInline)
    model=ProductAttachment


class ProductAdmin(admin.modelAdmin)
    inlines= [ProductInline,]
    model=Product
admin.site.register(Product, ProductAdmin)

forms.py

class ProductForm(models.form):
    class Meta:
        model=Product
        fields='__all__'

Views.py

class ProductCreateView(CreateView):
    model=Product
    form_class = ProductForm

但是我不知道如何获取表单中的所有文件字段。非常感谢任何帮助。谢谢

【问题讨论】:

    标签: django-models django-forms django-views django-class-based-views


    【解决方案1】:

    查看您的代码,您的 ProductForm 和 ProductCreateView 的模型是 Product,它不包含任何 FileFiled。您还需要为 ProductAttachment 创建表单/视图。

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 1970-01-01
      • 2017-10-26
      • 2012-04-19
      • 1970-01-01
      • 2014-05-24
      • 2018-10-11
      • 2019-04-23
      • 2011-11-27
      相关资源
      最近更新 更多