【问题标题】:Django GenericForeignKey: accessor clash when 2 models have same related_nameDjango GenericForeignKey:当 2 个模型具有相同的相关名称时访问器冲突
【发布时间】:2013-10-21 21:29:02
【问题描述】:

当我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 发生了冲突。我必须让它们独一无二吗?如果是这样,有没有办法做到这一点,而不必在每个子类中编写样板代码?

【问题讨论】:

  • 简短回答:related_name="%(app_label)s_%(class)s"。如果没有出现,我会写一个正确的答案(不是我没有时间)。
  • 谢谢@J.C.Leitão!您的快速回答解决了我的问题。

标签: python django django-models foreign-keys generic-foreign-key


【解决方案1】:

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

如果您在 ForeignKey 或 ManyToManyField 上使用 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 的反面

【讨论】:

    猜你喜欢
    • 2020-02-04
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2017-10-02
    • 2010-11-05
    • 1970-01-01
    • 2011-08-05
    • 2022-08-16
    相关资源
    最近更新 更多