以下是我需要的代码来执行inlineformset django。问题是我有两个具有相同ForeignKeys的属性,它返回一个Django错误
models.py
class Reference(models.Model):
user = models.ForeignKey(UserProfile, related_name="user_mariner", default=None)
verified_by = models.ForeignKey(UserProfile, related_name="verified_by", default=None)
company = models.ForeignKey(Company, default=None)
date = models.DateField(null=True, blank=True, default=None)
person_contacted = models.ForeignKey(PersonReference, default=None)
veracity_seagoing_history = models.NullBooleanField()
health_problem = models.NullBooleanField()
financial_liability = models.NullBooleanField()
rehiring_prospects = models.NullBooleanField()
character = models.TextField(null=True, blank=True, default=None)
comments = models.TextField(null=True, blank=True, default=None)
forms.py
class ReferenceForm(forms.ModelForm):
class Meta:
model = Reference
exclude = '__all__'
views.py
try:
reference = Reference.objects.filter(user=id)
ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fields='__all__', extra=3, can_delete=True )
reference_form = ReferenceFormSet(request.POST or None, instance=user_profile)
except:
print "%s - %s" % (sys.exc_info()[0], sys.exc_info()[1])
template = "application-profile/profile.html"
context_dict = {}
context_dict['reference_form'] = reference_form
return render(request, template, context_dict)
profile.html
{{ reference_form }}
错误
<type 'exceptions.ValueError'> - 'mariners_profile.Reference' has more than one ForeignKey to 'login.UserProfile'.
答案 0 :(得分:0)
您必须指定要用作父级“链接”的字段的名称。类似的东西:
ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fk_name='user', fields='__all__', extra=3, can_delete=True )