1

すると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

と をコメントアウトするTranscription2と問題がなくなるTranscription3ので、 related_names が衝突しているようです。それらを一意にする必要がありますか?もしそうなら、各子クラスに定型コードを書かなくてもこれを行う方法はありますか?

4

1 に答える 1

2

Django ドキュメントから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')

そのため、Transcription2.participant_content_type の逆を使用し%(app_label)_transcription2_as_participantてアクセスできます。

于 2013-10-14T10:37:10.110 に答える