我正在尝试向表单添加内联formset。这是重现错误的最小代码:
models.py
class Festival(Model):
desc = TextField(max_length=1000)
class FestivalAddress(Model):
festival = ForeignKey(Festival, related_name="addresses")
name = CharField(max_length="50")
urls.py
urlpatterns = patterns('',
url('^add/$', FestivalCreateView.as_view(), name='festival_add'),
)
views.py
class FestivalCreateView(CreateView):
model = Festival
form_class = FestivalForm
#Add FestivalAddressFormset to context here
forms.py
class FestivalAddressForm(ModelForm):
class Meta:
model = FestivalAddress
class FestivalForm(ModelForm):
class Meta:
model = Festival
FestivalAddressFormSet = inlineformset_factory(FestivalForm, FestivalAddress, form=FestivalAddressForm, extra=2)
抛出AttributeError:'ModelFormOptions'对象没有属性'get_parent_list'。我有点难过,因为我正在按照here on SO给出的解决方案。
编辑:我删除了FestivalCreateView对formset的使用,因为无论是否出现错误都会发生错误。
答案 0 :(得分:4)
看起来我对inlineformset_factory的调用不正确。根据{{3}},第一个参数应该是模型,而不是表格。
答案 1 :(得分:0)
应该是:
FestivalAddressFormSet = inlineformset_factory(Festival, FestivalAddress, form=FestivalAddressForm, extra=2)