當我syncdb
,我得到了許多錯誤是這樣的:Django的GenericForeignKey:訪問衝突時,2款具有相同related_name
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
問題就沒有了當我評論出Transcription2
和Transcription3
,所以看起來像related_names衝突。我必須讓它們獨一無二嗎?如果是這樣,是否有辦法做到這一點,而不必在每個子類中編寫樣板代碼?
簡短的回答:related_name =「%(app_label)S_ %(類)的」。如果沒有出現,我會寫一個正確的答案(不是我沒有時間)。 –
謝謝@J.C.Leitão!你的快速回答解決了我的問題。 – RexE