Django内联formset错误 [英] Django inline formset error

查看:308
本文介绍了Django内联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



$ $ $ $ $ $ $ $ $ $
$ c

$ b #Add FestivalAddressFormset到上下文

forms.py

  class FestivalAddressForm(ModelForm):
class Meta:
model = FestivalAddress

class FestivalForm(ModelForm):
class Meta:
model = Festiv al

FestivalAddressFormSet = inlineformset_factory(FestivalForm,FestivalAddress,form = FestivalAddressForm,extra = 2)

这将抛出AttributeError:'ModelFormOptions'对象没有属性'get_parent_list'。我有点困惑,因为我正在遵循

编辑:我删除了FestivalCreateView的表单集合的使用,因为错误发生在或没有它

看起来我对inlineformset_factory的调用不正确。根据文档,第一个参数应该是一个模型,而不是一个表单。


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.

解决方案

It looks like my call to inlineformset_factory was incorrect. According to the docs, the first argument should be a model, not a form.

这篇关于Django内联formset错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆