[Answered ]-Django GenericForeignKey: accessor clash when 2 models have same related_name

2πŸ‘

βœ…

From the Django docs 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’.

In this case, I think this will work:

    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')

So, using %(app_label)_transcription2_as_participant you can access the reverse of Transcription2.participant_content_type

πŸ‘€fasouto

Leave a comment