my models.py looks like this:
我的models.py看起来像这样:
class Member(models.Model):
name = models.CharField(max_length=255)
occupation = models.CharField(max_length=255)
class FamilyMember(models.Model):
name = models.CharField(max_length=255)
relation = models.CharField(max_length=255)
member = models.ForeignKey(Member)
Now, I am trying to create an inline form for family members inside the members form. To do that, I am using inlineformset_factory:
现在,我正在尝试为成员表单中的家庭成员创建内联表单。为此,我使用inlineformset_factory:
from django import forms from myapp.models import * from django.forms.models import inlineformset_factory
来自django的导入表单来自myapp.models import *来自django.forms.models import inlineformset_factory
class MemberForm(forms.ModelForm):
class Meta:
model = Member
fields = [
'name', 'occupation',
]
FamilyMemberFormSet = inlineformset_factory(Member, FamilyMember)
When I run the server, I get a weird error :
当我运行服务器时,我得到一个奇怪的错误:
"Calling modelformset_factory without defining 'fields' or "
django.core.exceptions.ImproperlyConfigured: Calling >modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.
What am I doing wrong? Thanks.
我究竟做错了什么?谢谢。
2 个解决方案
#1
14
I'm not sure but I think you forgot to pass the form to inlineformset_factory
:
我不确定,但我认为您忘记将表单传递给inlineformset_factory:
FamilyMemberFormSet = inlineformset_factory(Member, FamilyMember, form=MemberForm)
Or you should specify what fields should be included/excluded using either fields
parameter or exclude
.
或者,您应使用fields参数或exclude指定应包含/排除的字段。
See some examples in Django doc about inlineformset_factory.
请参阅Django doc中有关inlineformset_factory的一些示例。
#2
0
here you are:
这个给你:
FamilyMemberFormSet = inlineformset_factory(Member, FamilyMember, fields =['name', 'occupation',])
#1
14
I'm not sure but I think you forgot to pass the form to inlineformset_factory
:
我不确定,但我认为您忘记将表单传递给inlineformset_factory:
FamilyMemberFormSet = inlineformset_factory(Member, FamilyMember, form=MemberForm)
Or you should specify what fields should be included/excluded using either fields
parameter or exclude
.
或者,您应使用fields参数或exclude指定应包含/排除的字段。
See some examples in Django doc about inlineformset_factory.
请参阅Django doc中有关inlineformset_factory的一些示例。
#2
0
here you are:
这个给你:
FamilyMemberFormSet = inlineformset_factory(Member, FamilyMember, fields =['name', 'occupation',])