[Django] #19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring model fails

11 views
Skip to first unread message

Django

unread,
Feb 1, 2013, 8:59:37 PM2/1/13
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
------------------------------------------------+------------------------
Reporter: ramiro | Owner: nobody
Type: Bug | Status: new
Component: contrib.contenttypes | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/19722>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 2, 2013, 9:16:40 AM2/2/13
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
--------------------------------------+------------------------------------
Reporter: ramiro | Owner: ramiro
Type: Bug | Status: assigned
Component: contrib.contenttypes | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by ramiro):

* owner: nobody => ramiro
* status: new => assigned
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/19722#comment:1>

Django

unread,
Oct 8, 2015, 9:19:30 AM10/8/15
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
--------------------------------------+------------------------------------
Reporter: ramiro | Owner: ramiro
Type: Bug | Status: assigned
Component: contrib.contenttypes | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

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

[https://github.com/django/django/pull/5412 PR] to add a regression test.

--
Ticket URL: <https://code.djangoproject.com/ticket/19722#comment:2>

Django

unread,
Oct 8, 2015, 12:06:55 PM10/8/15
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
-------------------------------------+-------------------------------------

Reporter: ramiro | Owner: ramiro
Type: Bug | Status: assigned
Component: | Version: master
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* stage: Accepted => Ready for checkin


Comment:

Regression test case LGTM.

--
Ticket URL: <https://code.djangoproject.com/ticket/19722#comment:3>

Django

unread,
Oct 8, 2015, 3:04:37 PM10/8/15
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
-------------------------------------+-------------------------------------
Reporter: ramiro | Owner: ramiro
Type: Bug | Status: assigned
Component: | Version: master
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"4a7b58210defea33a428b748ccbc97ae8fd49838" 4a7b582]:
{{{
#!CommitTicketReference repository=""
revision="4a7b58210defea33a428b748ccbc97ae8fd49838"
Refs #19722 -- Added a test for querying generic relations of a parent
class.

Fixed in c9a96075fa02b6d52bec748ffdfb413688a15774.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19722#comment:4>

Django

unread,
Oct 8, 2015, 3:04:45 PM10/8/15
to django-...@googlegroups.com
#19722: GenericRelation in a subclass, filtering by a field of the GFK-declaring
model fails
-------------------------------------+-------------------------------------
Reporter: ramiro | Owner: ramiro
Type: Bug | Status: closed
Component: | Version: master
contrib.contenttypes |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: assigned => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/19722#comment:5>

Reply all
Reply to author
Forward
0 new messages