Django error message "Add related_name argument to definition"
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'. You have several foreign keys that django cannot generate unique names for.
You can help by adding the "related_name" arguments to the definitions of foreignkey fields in your models. For example:
content_type = ForeignKey(Topic, related_name='topic_content_type') See here for more details. http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

Example:
class Article(models.Model): author = models.ForeignKey('accounts.User') editor = models.ForeignKey('accounts.User') This will result in an error as Django tries to automatically create feedback for the accounts.User instances for each foreign key to user relationship, for example user.article_set . This method is ambiguous by default. Will user.article_set.all() link to user articles related to the author field or using the editor field?
Decision:
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 the user user instance 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 an author == useruser.editor_article_set-user.editor_article_set.all()will return a Queryset for all article objects that have an editor == user
"If the model has a ForeignKey, instances of the foreign key model will have access to the Manager, which returns all instances of the first model. By default, this manager is called FOO_set, where FOO is the name of the original model, lowercase."
But if you have several foreign keys in the model, django cannot generate unique names for the foreign key manager.
You can help by adding the "related_name" arguments to the definitions of foreignkey fields in your models.
See here: https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
Have the error message tell you:
Add the related_name argument to the definition for "creator".
But in my case, I am creating a separate application for some functions with the same name and field (copy / paste;)), that because of this type of error, I just deleted the old model and the code will work fine <w> Maybe full help for beginners like me :)
This is not the final answer to the question, but for someone it can solve the problem. I had the same error in my project after checking for a really old commit (going to the state of the detached head) and then restoring the code base. The solution was to delete all * .pyc files in the project.
If your models inherit from the same parent model, you must set a unique related_name in the parent ForeignKey . For example:
author = models.ForeignKey('accounts.User', related_name='%(app_label)s_%(class)s_related')
This is better explained in the docs.