Below are my needed codes for doing an inlineformset django. The problem is that I have two attributes with the same ForeignKeys and it returns a Django error
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 }}
Error
<type 'exceptions.ValueError'> - 'mariners_profile.Reference' has more than one ForeignKey to 'login.UserProfile'.
You have to specify the name of the field you want to use as the 'link' to the parent. Something like:
ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fk_name='user', fields='__all__', extra=3, can_delete=True )