Friday, October 26, 2012

Two Foreign Keys to same model require related_name definition

When creating a new model that requires two foreign keys to refer to the same other model, django requires you to define the related_name attribute for the ForeignKey field.

The related_name attribute specifies the name of the reverse relation from the User model back to your model.
If you don't specify a related_name, Django automatically creates one using the name of your model and the suffix _set.

Thus unless you specify a unique related_name for at least one of your FKs, your models won't validate throwing an error:
Accessor for field 'primary' clashes with related field '<FKModelname>.<modelname_set>'

 Ref: http://stackoverflow.com/questions/2642613/what-is-the-related-name-mean-in-django/2642645#2642645

No comments:

Post a Comment

Popular Posts