本文介绍了Django / South:在ManyToMany到自我关系中,为中间模型设置related_name参数的正确方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与,但是由于我暂时只是由于时间限制原型制作了一个应用程序, ve决定放下相关的表并重新设置迁移历史记录。

This is related to a question I asked yesterday about migrating a change from a ForeignKey to self to a ManyToManyField to self, however because I'm only prototyping an app for the time being/due to time constraints, I've decided to just drop the relevant tables and reset the migration history.

这些是相关的模型/字段:

These are the relevant models/fields:

class Person(models.Model):
    nominator = models.ManyToManyField('self', symmetrical=False,
            verbose_name=_('nominator'), through='Nomination', null=True,
            blank=True)


class Nomination(models.Model):
    nominee = models.ForeignKey(Person)
    nominator = models.ForeignKey(Person)

但是,这甚至不会生成初始迁移:

However, this doesn't even generate an initial migration:

$ ./manage.py schemamigration nominations --initial
CommandError: One or more models did not validate:
nominations.nomination: Accessor for field 'nominee' clashes with related field 'Person.nomination_set'. Add a related_name argument to the definition for 'nominee'.
nominations.nomination: Accessor for field 'nominator' clashes with related field 'Person.nomination_set'. Add a related_name argument to the definition for 'nominator'.

我按照说明添加一个 related_name 提名模型中 nominator nominee 字段的参数,如下所示:

I followed the instructions to add a related_name argument to the nominator and nominee fields on the Nomination model, like so:

class Nomination(models.Model):
    nominee = models.ForeignKey(Person, related_name=_('nominator'))
    nominator = models.ForeignKey(Person, related_name=_('nominee'))

这给了我一个不同的错误:

That gave me a different error:

$ ./manage.py schemamigration nominations --initial
CommandError: One or more models did not validate:
nominations.nomination: Accessor for field 'nominee' clashes with m2m field 'Person.nominator'. Add a related_name argument to the definition for 'nominee'.
nominations.nomination: Reverse query name for field 'nominee' clashes with m2m field 'Person.nominator'. Add a related_name argument to the definition for 'nominee'.

我不知道该怎么做。我感觉到我忘记了一些 Person 模型,但是我不知道这可能是什么,因为Django / South文档不是很可靠来到这种关系。

I'm not sure what to do from this point. I get the feeling that I've forgotten something the Person model, but I'm not sure what that could be since both the Django/South docs are not terribly forthcoming when it comes to this kind of relation.

推荐答案

只需使用字段名称以外的其他名称即可。有人像person_nominator和person_nominee。

Just use different names other than the fields names. Something like person_nominator and person_nominee.

我将此评论移到一个答案,以便问题不再没有答案。

I move this comment to an answer so that the question is no longer without an answer.

这篇关于Django / South:在ManyToMany到自我关系中,为中间模型设置related_name参数的正确方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:14