147
You have a number of foreign keys which django is unable to generate unique names for.
You can help out by adding “related_name” arguments to the foreignkey field definitions in your models.
Eg:
content_type = ForeignKey(Topic, related_name='topic_content_type')
See here for more.
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name
85
Example:
class Article(models.Model):
author = models.ForeignKey('accounts.User')
editor = models.ForeignKey('accounts.User')
This will cause the error, because Django tries to automatically create a backwards relation for instances of accounts.User
for each foreign key relation to user like user.article_set
. This default method is ambiguous. Would user.article_set.all()
refer to the user’s articles related by the author field, or by the editor field?
Solution:
class Article(models.Model):
author = models.ForeignKey('accounts.User', related_name='author_article_set')
editor = models.ForeignKey('accounts.User', related_name='editor_article_set')
Now, for an instance of user user
, there are two different manager methods:
-
user.author_article_set
—user.author_article_set.all()
will return a Queryset of all Article objects that have author == user -
user.editor_article_set
—user.editor_article_set.all()
will return a Queryset of all Article objects that have editor == user
Note:
This is an old example — on_delete
is now another required argument to models.ForeignKey
. Details at What does on_delete do on Django models?
- [Django]-Django index page best/most common practice
- [Django]-.filter() vs .get() for single object? (Django)
- [Django]-How do you serialize a model instance in Django?
14
“If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased.”
But if you have more than one foreign key in a model, django is unable to generate unique names for foreign-key manager.
You can help out by adding “related_name” arguments to the foreignkey field definitions in your models.
See here:
https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
- [Django]-Django's self.client.login(…) does not work in unit tests
- [Django]-Delete multiple objects in django
- [Django]-Django: remove a filter condition from a queryset
2
If your models are inheriting from the same parent model, you should set a unique related_name
in the parent’s ForeignKey. For example:
author = models.ForeignKey('accounts.User', related_name='%(app_label)s_%(class)s_related')
It’s better explained in the docs
- [Django]-Temporarily disable auto_now / auto_now_add
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
- [Django]-Auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'
2
If your models are inheriting from the same parent model, you should set a unique related_name in the parent’s ForeignKey. For example:
author = models.ForeignKey('accounts.User', related_name='%(app_label)s_%(class)s_related')
It’s better explained in th
- [Django]-Django render_to_string missing information
- [Django]-Best practice for Django project working directory structure
- [Django]-Disable session creation in Django
1
I had a similar problem when I was trying to code a solution for a table that would pull names of football teams from the same table.
My table looked like this:
hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)
making the below changes solved my issue:
hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='home_team')
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='away_team')
- [Django]-What's the purpose of Django setting ‘SECRET_KEY’?
- [Django]-Celery : Execute task after a specific time gap
- [Django]-Django File upload size limit
0
But in my case i am create a separate app for some functionality with same model name and field ( copy/paste ) that’s because of this type of error occurs i am just deleted the old model and code will work fine
May be help full for beginners like me
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-Django 1.8 KeyError: 'manager' on relationship
- [Django]-Django TypeError: 'RelatedManager' object is not iterable
-1
This isn’t an ultimate answer for the question, however for someone it may solve the problem.
I got the same error in my project after checking out a really old commit (going to detached head state) and then getting the code base back up to date. Solution was to delete all *.pyc files in the project.
- [Django]-Django datefield filter by weekday/weekend
- [Django]-Login Page by using django forms
- [Django]-Django modelform NOT required field
-3
Do as the error message instructs you to:
Add a related_name argument to the
definition for ‘creator’.
- [Django]-How do I reuse HTML snippets in a django view
- [Django]-Django FileField with upload_to determined at runtime
- [Django]-Django dynamic model fields