Opened 12 years ago
Closed 9 years ago
#19722 closed Bug (fixed)
GenericRelation in a subclass, filtering by a field of the GFK-declaring model fails
Reported by: | Ramiro Morales | Owned by: | Ramiro Morales |
---|---|---|---|
Component: | contrib.contenttypes | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | 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
This was report in a comment by user 'nator' to #11263, is a different issue to the one initially discussed in that ticket. Quoting:
Say we have a non-abstract superclass and a subclass, and we add a GR relationship to the subclass:
class Order(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() status = models.CharField(max_length=10, default='ordered') class Media(models.Model): order = generic.GenericRelation(Order) class Photo(Media): title = models.CharField() Photo.objects.filter(order__status='ordered').count() --> doesn't break, but always returns 0.
The basic problem is that when the Order object is created Django inserts it into the DB using the content_type of Photo, but when the select is run Django realizes Order actually lives in Media and tries to use that content_type. So the inserts all "work", but the count() never finds them. It's a mismatch between the content_type_id when it runs the insert and select.
After digging through django/config/contenttypes/generic.py a bit I feel like the correct answer is to change the inserts to use the content_type of the class that actually contains the m2m relationship. (Media, in this case).
There is a proposed fix attached to that ticket.
Change History (5)
comment:1 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 9 years ago
Has patch: | set |
---|
comment:5 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
This seems to be fixed in Django 1.6 as of c9a96075fa02b6d52bec748ffdfb413688a15774.
PR to add a regression test.