django错误消息“添加一个相关的_name参数”到定义上。

发布于 2025-02-10 00:38:26 字数 1107 浏览 3 评论 0 原文

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'.
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

凉城凉梦凉人心 2025-02-17 00:38:26

您有许多外国键,Django无法生成唯一的名称。

您可以通过在模型中的外国键字段定义中添加“ Related_name”参数来提供帮助。
例如:

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

有关更多信息,请参见此处。

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

写给空气的情书 2025-02-17 00:38:26

示例:

class Article(models.Model):
    author = models.ForeignKey('accounts.User')
    editor = models.ForeignKey('accounts.User')
    

这将导致错误,因为Django试图为 accounts.copers.sers.user 的每个外键与用户关系的实例自动创建一个向后关系,例如 user.article_set.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')

现在,对于用户用户的实例,有两个不同的管理方法:

  1. user.author_article_set - user.author_artile_set.all() /code>将返回具有作者==用户

    的所有文章对象的QuerySet

  2. user.editor_article_article_set - user.editor_article_set.all()将返回具有编辑器==用户

    的所有文章对象的问题


Note
这是一个旧的示例 - on_delete 现在是 modelsekey.foreignkey 的另一个必需的参数。详细信息 on_delete在django型号上有什么作用?

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?

如若梦似彩虹 2025-02-17 00:38:26

“如果模型具有外国基础,则外国关键模型的实例将可以访问返回第一个模型的所有实例的经理。默认情况下,此管理器被命名为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

青丝拂面 2025-02-17 00:38:26

如果您的模型是从同一父模型继承的,则应在父母的 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

花间憩 2025-02-17 00:38:26

如果您的模型是从同一父模型继承的,则应在父型外键中设置唯一的相关信息。例如:

author = models.ForeignKey('accounts.User', related_name='%(app_label)s_%(class)s_related')

在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:

author = models.ForeignKey('accounts.User', related_name='%(app_label)s_%(class)s_related')

It's better explained in th

梦过后 2025-02-17 00:38:26

当我试图为桌子编码解决方案时,我也有类似的问题,该桌子可以从同一张桌子上取出足球队的名字。
我的桌子看起来像这样:

hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)

进行以下更改解决了我的问题:

hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='home_team')
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='away_team')

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:

hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE)

making the below changes solved my issue:

hometeamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='home_team')
awayteamID = models.ForeignKey(Team, null=False, on_delete=models.CASCADE,related_name='away_team')
行至春深 2025-02-17 00:38:26

但是在我的情况下,我正在创建一个具有相同模型名称和字段(复制/粘贴;)的功能的单独应用程序,这是因为发生了这种错误,我只是被删除了旧模型,代码将正常工作

对于像我这样的初学者可能会有所帮助:)

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 :)

×眷恋的温暖 2025-02-17 00:38:26

这不是这个问题的最终答案,但是对于某人可能会解决问题。
在检查了一个非常旧的提交(将转到超级状态)之后,我在项目中遇到了同样的错误,然后将代码库备份最新。解决方案是删除项目中的所有 *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.

梦毁影碎の 2025-02-17 00:38:26

按错误消息指示您:

将相关的_name参数添加到
“创建者”的定义。

Do as the error message instructs you to:

Add a related_name argument to the
definition for 'creator'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文