#24940 closed Bug (fixed)
unhashable type: 'RelatedManager' when deleting a model using django admin that has GenericRelation and related_query_name
Reported by: | Federico Jaramillo Martínez | Owned by: | Markus Holtermann |
---|---|---|---|
Component: | contrib.admin | Version: | 1.8 |
Severity: | Release blocker | Keywords: | |
Cc: | Markus Holtermann | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Ok, a few things before he report:
- This is my first Django bug report (hope I do well)
- Didn't know how else to call the report, a better summary is appreciated.
The problem:
When using django admin to delete an instance of a model that has a GenericRelation with a related_query_name, an "unhashable type: 'RelatedManager'" error is thrown.
If the related_query_name is removed, the error disappears.
Example structure:
... class ModelA(models.Model): """Model with GenericForeignKey.""" content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey() class ModelB(models.Model): """Model with a GenericRelation and related_query_name.""" model_a = GenericRelation(ComponenteContable, related_query_name='model_a') class ModelC(models.Model): """Model with a GenericRelation and related_query_name.""" model_c = GenericRelation(ComponenteContable, related_query_name='model_c') class ModelD(models.Model): """Model with a GenericRelation and related_query_name.""" model_d = GenericRelation(ComponenteContable, related_query_name='model_d') ...
It is not possible to delete a model B, C or D instance in django admin if the have a related_query_name.
The moment you remove related_query_name, that Model's instances can be deleted.
Attachments (1)
Change History (7)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
Bisected to aa5ef0d4fc67a95ac2a5103810d0c87d8c547bac. Attaching a regression test for Django's test suite.
by , 10 years ago
Attachment: | 24940-test.diff added |
---|
comment:3 by , 10 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:4 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
A bit of debugging shows that when deleting an instance, django admin finds all the instances that are related to it in order to delete them as well.
Problem is the dictionary created to store all the related instances to be deleted uses those same instances as keys in order to recursively find the whole tree.
When GenericRelation has a related_query_name, it generates a dynamic "RelatedManager" class that is not hashable.
This all happens in a NestedObjects class defined in django.contrib.admin.utils
From there, I got lost.