首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django GenericForeignKey:当两个模型具有相同的related_name时访问器冲突

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

Stack Overflow用户
提问于 2013-10-14 10:23:11
回答 1查看 939关注 0票数 1

当我syncdb时,我会遇到很多这样的错误:

代码语言:javascript
复制
   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'.

我的模特已经有了相关的名字:

代码语言:javascript
复制
# 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冲突。我一定要让它们独一无二吗?如果是这样的话,是否有一种无需在每个子类中编写样板代码的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-14 10:37:10

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

如果在related_name或ManyToManyField上使用ForeignKey属性,则必须始终为字段指定唯一的反向名称。这通常会在抽象基类中引起问题,因为该类上的字段包含在每个子类中,每次属性(包括related_name)的值完全相同。 为了解决这个问题,在抽象基类中使用related_name时,名称的一部分应该包含‘%(App_label)’和‘%(类)’。

在这种情况下,我认为这是可行的:

代码语言:javascript
复制
    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的反向

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19358144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档