gpt4 book ai didi

python - Django 通用外键 : accessor clash when 2 models have same related_name

转载 作者:行者123 更新时间:2023-12-01 05:34:46 24 4
gpt4 key购买 nike

当我 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 文档 https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name :

If you are using the related_name attribute on a ForeignKey or ManyToManyField, you must always specify a unique reverse name for the field. This would normally cause a problem in abstract base classes, since the fields on this class are included into each of the child classes, with exactly the same values for the attributes (including related_name) each time.

To work around this problem, when you are using related_name in an abstract base class (only), part of the name should contain '%(app_label)s' and '%(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 的反向内容

关于python - Django 通用外键 : accessor clash when 2 models have same related_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19358144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com