[Django] #30368: prefetch_related does not work for GenericForeignKey field when its primary key is also a foreign key

7 views
Skip to first unread message

Django

unread,
Apr 15, 2019, 2:20:08 AM4/15/19
to django-...@googlegroups.com
#30368: prefetch_related does not work for GenericForeignKey field when its primary
key is also a foreign key
-------------------------------------+-------------------------------------
Reporter: Vinny Do | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) | Keywords: prefetch_related
Severity: Normal | genericforeignkey
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
**Steps to reproduce**

Create `Base` model with an `AutoField` primary key
{{{#!python
class Base(models.Model):
title = models.TextField()
}}}

Create a model where the primary key is also a foreign key
{{{#!python
class Extended(models.Model):
base = models.OneToOneField(Base, on_delete=models.CASCADE,
primary_key=True)
}}}

Create model with `GenericForeignKey`
{{{#!python
class Comment(models.Model):
content_type = models.ForeignKey(ContentType,
on_delete=models.CASCADE)
object_pk = models.TextField()
content_object = GenericForeignKey(ct_field="content_type",
fk_field="object_pk")
}}}

Prefetch the GenericForeignKey field `content_object` expecting it to have
a value but get `None` instead.
{{{#!python
# Setup
base = Base.objects.create(title="foo")
extended = Extended.objects.create(base=base)
Comment.objects.create(content_object=extended)

# Exercise
comment = Comment.objects.prefetch_related("content_object").get()
print(comment.content_object)
}}}

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

Django

unread,
Apr 15, 2019, 2:41:39 AM4/15/19
to django-...@googlegroups.com
#30368: prefetch_related does not work for GenericForeignKey field when its primary
key is also a foreign key
-------------------------------------+-------------------------------------
Reporter: Vinny Do | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage:
genericforeignkey | Unreviewed
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


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

Django

unread,
Apr 15, 2019, 11:18:23 AM4/15/19
to django-...@googlegroups.com
#30368: Prefetch related is not working when used GFK for model that uses FK as PK.
-------------------------------------+-------------------------------------
Reporter: Vinny Do | Owner: Vinny Do
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
genericforeignkey |
Has patch: 1 | Needs documentation: 0

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

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


Comment:

[https://github.com/django/django/pull/11234 PR]

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

Django

unread,
Apr 16, 2019, 3:49:50 AM4/16/19
to django-...@googlegroups.com
#30368: Prefetch related is not working when used GFK for model that uses FK as PK.
-------------------------------------+-------------------------------------

Reporter: Vinny Do | Owner: Vinny Do
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: prefetch_related | Triage Stage: Accepted
genericforeignkey |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"a4055adf702d086334a9ab2ca25a5e41e896a4fb" a4055adf]:
{{{
#!CommitTicketReference repository=""
revision="a4055adf702d086334a9ab2ca25a5e41e896a4fb"
Fixed #30368 -- Fixed prefetch_related() for GenericForeignKey when PK is
also a FK.
}}}

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

Reply all
Reply to author
Forward
0 new messages