Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django error message "Add a related_name argument to the definition"

Tags:

django

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'.
like image 304
zjm1126 Avatar asked Apr 09 '10 09:04

zjm1126


3 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

like image 189
John Mee Avatar answered Oct 17 '22 11:10

John Mee


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:

  1. user.author_article_setuser.author_article_set.all() will return a Queryset of all Article objects that have author == user

  2. user.editor_article_setuser.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?

like image 34
Mark Chackerian Avatar answered Oct 17 '22 10:10

Mark Chackerian


"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

like image 14
hufei Avatar answered Oct 17 '22 09:10

hufei