Django inline formset error

穿精又带淫゛_ 提交于 2019-12-24 17:48:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!