Opened 18 years ago
Closed 18 years ago
#2573 closed defect (invalid)
Relating two models to the same model using generic relations with the same parameters causes conflicts
Reported by: | Chris Long | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | generic relations, database |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using the following models:
class Group(models.Model): ... row_level_permissions_owned = models.GenericRelation(RowLevelPermission, object_id_field="owner_id", content_type_field="owner_ct", related_name="owner") ... class User(models.Model): ... row_level_permissions_owned = models.GenericRelation(RowLevelPermission, object_id_field="owner_id", content_type_field="owner_ct", related_name="owner") ... class RowLevelPermission(models.Model): class RowLevelPermission(models.Model): ... owner_id = models.PositiveIntegerField("'Owner' ID") owner_ct = models.ForeignKey(ContentType, verbose_name="'Owner' content type", related_name="owner_ct") ... model = models.GenericForeignKey(fk_field='model_id', ct_field='model_ct') owner = models.GenericForeignKey(fk_field='owner_id', ct_field='owner_ct')
Causes the following errors:
auth.user: Accessor for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'. auth.user: Reverse query name for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'. auth.group: Accessor for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'. auth.group: Reverse query name for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'.
By changing the two related_names to be different from another will stop this error. As long as the two related_names are the same (no matter what they are) the error happens.
From what I've been able to determine this happens because Django believes this to be a M2M relationship that contributes an attribute to the related class (which it does not) and having two attributes of the same name for two different relationships on the same object causes it to believe there is an error.
Doesn't look like a bug to me unless I'm missing something.
You can't have two generic or foreign keys with identical
related_name
s pointing to the same model.