D:\zjm_code\basic_project>python manage.py syncdb
Error: One or more models did not validate:
topics.topic: Accessor for field 'content_type' clashes with related field 'Cont
entType.topic_set'. Add a related_name argument to the definition for 'content_t
ype'.
topics.topic: Accessor for field 'creator' clashes with related field 'User.crea
ted_topics'. Add a related_name argument to the definition for 'creator'.
topics.topic: Reverse query name for field 'creator' clashes with related field
'User.created_topics'. Add a related_name argument to the definition for 'creato
r'.
topicsMap.topic: Accessor for field 'content_type' clashes with related field 'C
ontentType.topic_set'. Add a related_name argument to the definition for 'conten
t_type'.
topicsMap.topic: Accessor for field 'creator' clashes with related field 'User.c
reated_topics'. Add a related_name argument to the definition for 'creator'.
topicsMap.topic: Reverse query name for field 'creator' clashes with related fie
ld 'User.created_topics'. Add a related_name argument to the definition for 'cre
ator'.
- 
                    14If you don't know what a "related_name" argument is, the error message instructions aren't very useful. The Django "Related objects" docs don't necessarily help; they don't define a related_name nor make it clear that you can invent any value you want for your related_name. – Travis Wilson Jun 13 '13 at 21:16
9 Answers
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
 
    
    - 50,179
- 34
- 152
- 186
- 
                    8
- 
                    3
- 
                    11`related_name='+'` does suppress the error but note its purpose - to opt out of the "backward" link. IE: `fooo_set` usage on a foreign instance will be unable to find the instances here which are using it. – John Mee Aug 23 '12 at 05:50
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?
 
    
    - 21,866
- 6
- 108
- 99
- 
                    
- 
                    This is a dramatically better answer than the accepted. Thank you for the clean explanation and example! – Ethan Z Dec 23 '20 at 03:32
- 
                    If you could elaborate on the `on_delete` argument as well, that would be good, @MarkCharkerian – NeilG Sep 07 '21 at 06:27
- 
                    1
"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
 
    
    - 141
- 1
- 3
- 
                    1Half the battle is understanding what the error message means. I found this simple explanation (with the link) very helpful. – Bobble Sep 14 '15 at 10:48
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
 
    
    - 192
- 1
- 9
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
 
    
    - 21
- 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')
 
    
    - 184
- 1
- 12
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 :) 
 
    
    - 4,231
- 1
- 33
- 40
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.
 
    
    - 1,094
- 2
- 14
- 23
Do as the error message instructs you to:
Add a related_name argument to the definition for 'creator'.
 
    
    - 84,407
- 47
- 135
- 168
- 
                    6Yeah, great, I'll do then `related_name="foobar"`? Is that ok? Maybe! How do I know? I don't! – blueFast Dec 18 '15 at 08:51
 
    