繁体   English   中英

Django GenericForeignKey:当两个模型具有相同的related_name时,访问器冲突

[英]Django GenericForeignKey: accessor clash when 2 models have same related_name

当我syncdb ,我收到很多这样的错误:

   transcription.transcription1: Accessor for field 'participant_content_type' clashes with related field 'ContentType.auxi
    liary_model_as_participant'. Add a related_name argument to the definition for 'participant_content_type'.
    transcription.transcription1: Reverse query name for field 'participant_content_type' clashes with related field 'Conten
    tType.auxiliary_model_as_participant'. Add a related_name argument to the definition for 'participant_content_type'.

我的模型已经有相关名称:

# my base class which I intend to inherit from in many places.
# Many types of AuxiliaryModel will point at participant/match objects.:
class AuxiliaryModel(models.Model):
    participant_content_type = models.ForeignKey(ContentType,
                                                 editable=False,
                                                 related_name = 'auxiliary_model_as_participant')
    participant_object_id = models.PositiveIntegerField(editable=False)
    participant = generic.GenericForeignKey('participant_content_type',
                                            'participant_object_id',
                                            )

    match_content_type = models.ForeignKey(ContentType,
                                           editable=False,
                                           related_name = 'auxiliary_model_as_match')
    match_object_id = models.PositiveIntegerField(editable=False)
    match = generic.GenericForeignKey('match_content_type',
                                      'match_object_id',
                                      )

    class Meta:
        abstract = True


class Transcription(AuxiliaryModel):

    transcription = models.TextField(max_length=TRANSCRIPTION_MAX_LENGTH,
                                        null=True)

    class Meta:
        abstract = True

class Transcription1(Transcription):
    pass

class Transcription2(Transcription):
    pass

class Transcription3(Transcription):
    pass

当我注释掉Transcription2Transcription3 ,问题就消失了,所以related_names似乎冲突了。 我必须使它们独特吗? 如果是这样,有没有一种方法而不必在每个子类中编写样板代码?

从Django docs https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

如果在ForeignKey或ManyToManyField上使用related_name属性,则必须始终为该字段指定唯一的反向名称。 这通常会在抽象基类中引起问题,因为此类的字段包含在每个子类中,并且每次属性(包括related_name)的值都完全相同。

要变通解决此问题,当您在抽象基类(仅)中使用related_name时,部分名称应包含'%(app_label)s'和'%(class)s'。

在这种情况下,我认为这会起作用:

    participant_content_type = models.ForeignKey(ContentType,
                                             editable=False,
                                             related_name = '%(app_label)s_%(class)s_as_participant')
    match_content_type = models.ForeignKey(ContentType,
                                       editable=False,
                                       related_name = '%(app_label)s_%(class)s_model_as_match')

因此,使用%(app_label)_transcription2_as_participant您可以访问Transcription2.participant_content_type的%(app_label)_transcription2_as_participant

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM