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'.
Joh*_*Mee 103
你有许多外键,django无法为其生成唯一的名称.
您可以通过向模型中的foreignkey字段定义添加"related_name"参数来提供帮助.例如:
content_type = ForeignKey(Topic, related_name='topic_content_type')
请看这里了解更多. http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name
Mar*_*ian 35
例:
class Article(models.Model):
    author = models.ForeignKey('accounts.User')
    editor = models.ForeignKey('accounts.User')
这将导致错误,因为Django尝试accounts.User为每个外键关系的实例自动创建向后关系user.article_set.此默认方法不明确.会user.article_set.all()引用作者字段或编辑器字段相关的用户文章吗?
解:
class Article(models.Model):
    author = models.ForeignKey('accounts.User', related_name='author_article_set')
    editor = models.ForeignKey('accounts.User', related_name='editor_article_set')
现在,对于用户的实例user,有两种不同的管理器方法:  
user.author_article_set- user.author_article_set.all()将返回具有author == user的所有Article对象的Queryset
user.editor_article_set- user.editor_article_set.all()将返回具有editor == user的所有Article对象的Queryset
小智 13
"如果模型具有ForeignKey,则外键模型的实例将有权访问返回第一个模型的所有实例的Manager.默认情况下,此Manager名为FOO_set,其中FOO是源模型名称,小写."
但是如果模型中有多个外键,则django无法为外键管理器生成唯一的名称.
您可以通过向模型中的foreignkey字段定义添加"related_name"参数来提供帮助.
见这里:https: //docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
| 归档时间: | 
 | 
| 查看次数: | 47714 次 | 
| 最近记录: |