Django: reverse accessors for foreign keys

I have two Django models that inherit from the base class:

- Request - Inquiry - Analysis 

The request has two foreign keys for 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 get an error

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

All that I read suggests that setting related_name should prevent a collision, but I still get the same error. Can anyone think why this is happening? Thank!

+50
python django
Mar 20 '14 at 16:00
source share
1 answer

The associated name will ensure that the fields do not contradict each other, but you have two models, each of which has both of these fields. You need to specify the name of a specific model in each, which you can do with a special string replacement :

  create_user = models.ForeignKey(User, related_name='%(class)s_requests_created') 
+69
Mar 20 '14 at 16:12
source share



All Articles