django错误消息“添加一个相关的_name参数”到定义上。
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'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您有许多外国键,Django无法生成唯一的名称。
您可以通过在模型中的外国键字段定义中添加“ Related_name”参数来提供帮助。
例如:
有关更多信息,请参见此处。
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:
See here for more.
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name
示例:
这将导致错误,因为Django试图为
accounts.copers.sers.user
的每个外键与用户关系的实例自动创建一个向后关系,例如user.article_set.set
。此默认方法模棱两可。user.Article_set.all()
会请参考作者字段或编辑器字段相关的用户文章吗?解决方案:
现在,对于用户
用户
的实例,有两个不同的管理方法:user.author_article_set
-user.author_artile_set.all() /code>将返回具有作者==用户
的所有文章对象的QuerySet
user.editor_article_article_set -
user.editor_article_set.all()
将返回具有编辑器==用户的所有文章对象的问题
Note :
这是一个旧的示例 -
on_delete
现在是modelsekey.foreignkey
的另一个必需的参数。详细信息 on_delete在django型号上有什么作用?Example:
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 likeuser.article_set
. This default method is ambiguous. Woulduser.article_set.all()
refer to the user's articles related by the author field, or by the editor field?Solution:
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 == useruser.editor_article_set
—user.editor_article_set.all()
will return a Queryset of all Article objects that have editor == userNote:
This is an old example —
on_delete
is now another required argument tomodels.ForeignKey
. Details at What does on_delete do on Django models?“如果模型具有外国基础,则外国关键模型的实例将可以访问返回第一个模型的所有实例的经理。默认情况下,此管理器被命名为foo_set,其中foo是源模型名称,较低量。”
但是,如果您的模型中有多个外国密钥,Django将无法为外国钥匙经理生成唯一的名称。
您可以通过在模型中的外国键字段定义中添加“ Related_name”参数来提供帮助。
请参阅此处:
https://djangoproject.com/en/ dev/topics/db/queries/#rasshiphiphhips-backward
"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
如果您的模型是从同一父模型继承的,则应在父母的 efirnekey 中设置唯一的
Related_name
。例如:wuter = models.foreignkey('counduts.user',rections_name ='%(app_label)s _%(class)s_Realed')
在 docs
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
如果您的模型是从同一父模型继承的,则应在父型外键中设置唯一的相关信息。例如:
在TH中更好地解释了
If your models are inheriting from the same parent model, you should set a unique related_name in the parent's ForeignKey. For example:
It's better explained in th
当我试图为桌子编码解决方案时,我也有类似的问题,该桌子可以从同一张桌子上取出足球队的名字。
我的桌子看起来像这样:
进行以下更改解决了我的问题:
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:
making the below changes solved my issue:
但是在我的情况下,我正在创建一个具有相同模型名称和字段(复制/粘贴;)的功能的单独应用程序,这是因为发生了这种错误,我只是被删除了旧模型,代码将正常工作
对于像我这样的初学者可能会有所帮助:)
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 :)
这不是这个问题的最终答案,但是对于某人可能会解决问题。
在检查了一个非常旧的提交(将转到超级状态)之后,我在项目中遇到了同样的错误,然后将代码库备份最新。解决方案是删除项目中的所有 *pyc文件。
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.
按错误消息指示您:
Do as the error message instructs you to: