问题
I'm trying to add an inline formset to a form. Here's the minimal code for reproducing the error:
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)
This throws AttributeError: 'ModelFormOptions' object has no attribute 'get_parent_list'. I'm a bit stumped, as I'm following the solution given here on SO.
Edit: I removed FestivalCreateView's usage of the formset because the error occurs with or without it.
回答1:
It looks like my call to inlineformset_factory was incorrect. According to the docs, the first argument should be a model, not a form.
回答2:
Should have been:
FestivalAddressFormSet = inlineformset_factory(Festival, FestivalAddress, form=FestivalAddressForm, extra=2)
来源:https://stackoverflow.com/questions/11198638/django-inline-formset-error