【发布时间】:2022-01-01 17:38:16
【问题描述】:
我再次陷入模型表单集。我有以下表格:
ArticleFormSet = modelformset_factory(Article, fields=('title', 'pub_date'), extra=3)
这个模型表单集的视图:
class ArticleCreateView(CreateView):
model = Article
form_class = ArticleFormSet
template_name = 'article_form_view.html'
success_url = "/"
在此处阅读文档:https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/#model-formsets 我明白了,我可以使用 ArticleFormSet 而不是 form_class 属性的标准形式。
所以基本上,我所期望的是,这应该为同一个文章模型显示 3 个表单,并且我应该能够同时创建三篇文章。
但我收到“TypeError: init() got an unexpected keyword argument 'instance' ”。
所以问题是,这是它应该如何工作的吗?或者同时创建几篇文章的正确方法是什么?
【问题讨论】:
-
您能否指定遇到错误时您正在做什么并提供完整的错误回溯?
-
其实我在做什么在这里并不重要。我想要的只是在基于通用类的视图中显示模型表单集,在这种特殊情况下为 CreateView。我的意思是甚至可以将模型表单集与 CBV 一起使用吗?
标签: django django-forms formset