的型号:的ModelForm ForeignKey的用在继承的模型
class Human(models.Model):
UNIQUE = models.CharField(max_length=10)
name = models.CharField(max_length=30)
father = models.ForeignKey('Human', null=True, blank=True)
mother = models.ForeignKey('Human', null=True, blank=True)
class Person(Human):
email = models.EmailField()
而现在,我试图做的ModelForm:
class PersonForm(ModelForm):
class Meta:
model = Person
fields = ('UNIQUE','name','email')
截止本 - 完美的作品。
,现在我想补充两个字段:父亲和母亲
如果个人已经拥有的父亲(和/或母亲) - 只显示名称。如果不是 - 显示字段(或两个字段),用户必须输入UNIQUE。
我堆在刚刚加场:我尝试爸爸妈妈加入到类元字段:
class PersonForm(ModelForm):
class Meta:
model = Person
fields = ('UNIQUE','name', 'email', 'father', 'mother')
错误说:
Exception Type: AttributeError
Exception Value: 'Person' object has no attribute 'father_id'
这是不正确的,因为它有这个属性(从人类继承)....任何提示?
大部分的解决方案分进入choiceview,但我不希望显示的可能性 - 只要输入唯一的,如果是在基地 - 显示,如果不是 - 什么都不做
编辑
因为讨论错误的方式 - 模型人类和模型人肯定是好的 - 在管理一切正常 - 但我创建了输入数据的界面(我不想从管理模块等其他可能性中选择父/母)
编辑ED2 非常重要的修改:
father = models.ForeignKey('Human', related_name="father", null=True, blank=True)
mother = models.ForeignKey('Human', related_name="mother", null=True, blank=True)
错误消失,并在形式出现的母亲/父亲的ID - 这是非常接近它变成UNIQUE
'father = models.ForeignKey,null = True,blank = True)' - 非常奇怪的行。它确切的代码? – stalk 2014-09-19 09:00:54
对不起,我的错误 - 当然不是...... – 2014-09-19 09:20:36
你为什么在'ModelForm'中'model = Person'而不是'model = Human'?错误地告诉,发生了什么:人模型没有'父'字段 – stalk 2014-09-19 09:26:57