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'.
score:1
Accepted answer
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 )
Credit To: stackoverflow.com
Related Query
- Django Inlineformset with two same foreignkeys
- Combine two unrelated tables/models with same primary key in Django
- django two ModelForms with same field name on one template
- Django merge two migrations with the same ID?
- django two ForeignKeys to same model - admin error
- How to run two django apps in same server with different dynamic DJANGO_SETTINGS_MODULE env variables
- Django - saving two records at the same time with a foreign key relation
- How to use two tables with same name in two different schema in mysql with django
- Two Django projects with same database - permission denied for relation django_migrations
- Defining two api end points with same generic class view Django Rest Framework?
- django multiple ModelForms with the same model and ForeignKeys
- Django forms two fields with same same value one being unique with default value and other being hidden field
- django two apps with same urls in each url's.py file
- How to calculate date between different fields in two querysets with same foreign key in Django
- Django Urls - two views with same regular expression
- Django CreateView with two forms at same page
- Django - two form tags on single page with same action attribute
- Error with two same views in different apps in django project
- How to compare two JSON objects with the same elements in a different order equal?
- Select between two dates with Django
- How to create a Django queryset filter comparing two date fields in the same model
- How to handle request.GET with multiple variables for the same parameter in Django
- Django model with 2 foreign keys from the same table
- Django Admin: how to display fields from two different models in same view?
- django submit two different forms with one submit button
- How to get two random records with Django
- How do I create a Django model with ForeignKeys which does not cascade deletes to its children?
- How to make two django projects share the same database
- Django form with fields from two different models
- I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?
More Query from same tag
- Django Left Outer Join
- Django - Haystack Query Serialization
- if add new blog in database than sent mail to all subscriber to notified current blog in Django
- Django: how to trigger creation of ContentTypes for new models?
- Django - Set different id for each radio button choice
- Django bootstrap files not found
- What is the best way to dynamically load a specific app in django depending of another class?
- What does related_name do?
- Django unit test response context is None
- Join two querysets in django but keeping their order?
- Django Heroku and postgresql
- what is difference between success_url and get_success_url
- Java Projects on Django?
- Problem with writing a django rest serializer
- Python Django: Time Zone Conversion