具有两个相同外键的 Django Inlineformset

Django Inlineformset with two same foreignkeys

下面是我执行内联表单集 Django 所需的代码。问题是我有两个具有相同外键的属性,它 returns 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'.

您必须指定要用作父级 'link' 的字段名称。类似于:

ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fk_name='user', fields='__all__', extra=3, can_delete=True )