Opened 3 years ago

Closed 7 weeks ago

#19722 closed Bug (fixed)

GenericRelation in a subclass, filtering by a field of the GFK-declaring model fails

Reported by: ramiro Owned by: ramiro
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 3 years ago by ramiro

  • Owner changed from nobody to ramiro
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 weeks ago by timgraham

  • Has patch set

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

PR to add a regression test.

comment:3 Changed 7 weeks ago by charettes

  • Triage Stage changed from Accepted to Ready for checkin

Regression test case LGTM.

comment:4 Changed 7 weeks 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 7 weeks ago by timgraham

  • Resolution set to fixed
  • Status changed from assigned to closed
Note: See TracTickets for help on using tickets.
Back to Top