Opened 5 years ago

Closed 3 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: master
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


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()

--> 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/ 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 Changed 5 years ago by Ramiro Morales

Owner: changed from nobody to Ramiro Morales
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by Tim Graham

Has patch: set

This seems to be fixed in Django 1.6 as of c9a96075fa02b6d52bec748ffdfb413688a15774.

PR to add a regression test.

comment:3 Changed 3 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

Regression test case LGTM.

comment:4 Changed 3 years ago by Tim Graham <timograham@…>

In 4a7b582:

Refs #19722 -- Added a test for querying generic relations of a parent class.

Fixed in c9a96075fa02b6d52bec748ffdfb413688a15774.

comment:5 Changed 3 years ago by Tim Graham

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top