2010-04-09 35 views
47
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'. 
+3

添加模型 – 2010-04-09 09:49:19

+6

没有尝试以下错误消息的指令代码的所有文章对象的查询集? – 2010-04-09 09:50:45

+10

如果您不知道“related_name”参数是什么,那么错误消息说明不是很有用。 Django“相关对象”文档不一定有帮助;他们没有定义related_name,也没有明确说明你可以为你的related_name创建任何你想要的值。 – 2013-06-13 21:16:39

回答

80

您有许多外键,django无法为其生成唯一的名称。

您可以通过将“related_name”参数添加到模型中的foreignkey字段定义中来提供帮助。 如:

content_type = ForeignKey(Topic, related_name='topic_content_type') 

在这里看到更多。 http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

+6

或只是'related_name ='+'' – 2012-05-12 13:35:25

+3

@HuckleberryFinn,'+'是做什么的? – 2012-07-05 15:50:57

+0

@KirkWoll传递相关字段冲突/冲突错误。 – 2012-07-05 16:18:04

0

不要作为错误信息指示您:

一个related_name参数添加到 定义“创造者”。

+0

是的,太棒了,那么我会这样做'related_name =“foobar”'?这可以吗?也许!我怎么知道?我不! – dangonfast 2015-12-18 08:51:49

12

“如果模型具有ForeignKey,外键模型的实例将有权访问Manager,该Manager将返回第一个模型的所有实例。默认情况下,该Manager被命名为FOO_set,其中FOO是源型号名称,小写。“

但是,如果模型中有多个外键,则django无法为外键管理器生成唯一的名称。
您可以通过向模型中的foreignkey字段定义添加“related_name”参数来提供帮助。

在这里看到: https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

+1

一半的争斗是理解错误信息的含义。我发现这个简单的解释(与链接)非常有帮助。 – Bobble 2015-09-14 10:48:55

0

但在我的情况下,我创建具有相同的型号名称和领域的一些功能的单独的应用程序(复制/粘贴)),这是因为这种类型的错误发生时我只是的删除旧的模型和代码将正常工作
可能会帮助像我这样的初学者充分:)

0

这不是问题的最终答案,但对于某人来说,它可能会解决问题。 在检查出一个非常旧的提交(去分离头状态),然后让代码库返回到最新状态之后,我在项目中得到了同样的错误。解决方案是删除项目中的所有* .pyc文件。

6

例子:

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的情况下,有两种不同的管理器方法:

  1. user.author_article_set - user.author_article_set.all()将返回拥有所有文章对象的查询集作者==用户

  2. user.editor_article_set - user.editor_article_set.all() wil升返回有编辑==用户

相关问题