1

这与我昨天问的关于将更改从 ForeignKey 迁移到 self 到 ManyToManyField 到 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'.

我按照说明向模型上的and字段添加related_name参数,如下所示:nominatornomineeNomination

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 文档在涉及到这种关系时都不是很容易接受。

4

1 回答 1

1

只需使用字段名称以外的其他名称。像 person_nominator 和 person_nominee 这样的东西。

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

于 2014-08-06T08:20:37.763 回答