Django / South:在ManyToMany上为self关系设置中间模型的related_name参数的正确方法是什么?

时间:2013-10-24 14:04:41

标签: django django-models django-south

这与question I asked yesterday about migrating a change from a ForeignKey to self to a ManyToManyField to self有关,但是因为我暂时只是因为时间限制而对应用程序进行原型设计,所以我决定放弃相关表并重置迁移历史记录。

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

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)

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

$ ./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模型上的nominatornominee字段添加Nomination参数,如下所示:

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

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

$ ./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文档都不是非常即将发布。

1 个答案:

答案 0 :(得分:1)

只使用字段名称以外的其他名称。像person_nominator和person_nominee。

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