172

I have two Django models which inherit from a base class:

- Request
    - Inquiry
    - Analysis

Request has two foreign keys to the built-in User model.

create_user = models.ForeignKey(User, related_name='requests_created')
assign_user = models.ForeignKey(User, related_name='requests_assigned')

For some reason I'm getting the error

Reverse accessor for 'Analysis.assign_user' clashes with reverse accessor for 'Inquiry.assign_user'.

Everything I've read says that setting the related_name should prevent the clash, but I'm still getting the same error. Can anyone think of why this would be happening? Thanks!

serverpunk
  • 10,665
  • 15
  • 61
  • 95
  • 2
    Can you please post your model definitions, including `Inquiry` and `Analysis` and their relationship to `Request` – dm03514 Mar 20 '14 at 16:09
  • [updated link](https://docs.djangoproject.com/en/1.9/topics/db/models/#be-careful-with-related-name) for related name in Django 1.9 – Seb May 10 '16 at 21:38

1 Answers1

217

The related_name would ensure that the fields were not conflicting with each other, but you have two models, each of which has both of those fields. You need to put the name of the concrete model in each one, which you can do with some special string substitution:

 create_user = models.ForeignKey(User, related_name='%(class)s_requests_created')
phoenix
  • 7,988
  • 6
  • 39
  • 45
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 2
    @MatthewDrill I submitted an edit to correct it, in the meantime try https://docs.djangoproject.com/en/1.10/topics/db/models/#be-careful-with-related-name-and-related-query-name – Ivan Aug 04 '17 at 00:24
  • 1
    Newer doc link: https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ForeignKey.related_name – roeland May 28 '21 at 12:03