[Django] #21422: prefetch_related does not document restriction

5 views
Skip to first unread message

Django

unread,
Nov 11, 2013, 7:32:48 AM11/11/13
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
--------------------------------------+--------------------
Reporter: pab@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
I have just been trying to track down a performance issue in my
application using Django 1.6. The solution was to use prefetch_related(),
bit it didn't quite work as expected. The documentation was great as far
as it went and so I found the necessary function with little fuss - well
done! However it didn't warn me that prefetch_related() must be
restricted to a homogenous set of results.

In particular, I had query that needed to fetch data for objects found
through a GenericForeignKey and then a ManyToManyField. While the pre-
fetch claimed it had worked, it only actually queried the ManyToManyField
results for the first matching ContentType of the GenericForeignKey.
Looking at the code, this is a deliberate design decision, but it is not
documented anywhere I could see.

I understand why this restriction is in place and so am not asking for a
feature enhancement. Instead, I think the documentation for this function
needs to make clear that pre-fetching objects referenced by
GenericForeignKeys is only supported if the query has restricted the key
to one ContentType.

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

Django

unread,
Nov 11, 2013, 10:00:37 AM11/11/13
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
--------------------------------------+------------------------------------

Reporter: pab@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.6
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 loic84):

* cc: loic@… (added)
* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

I can confirm that the implementation relies on the assumption that all
the retrieved objects are homogenous.

This limitation should be documented.

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

Django

unread,
Jul 12, 2015, 11:29:48 AM7/12/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
--------------------------------------+------------------------------------

Reporter: pab@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by ubernostrum):

* easy: 0 => 1


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

Django

unread,
Jul 25, 2015, 4:47:57 AM7/25/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner:
Type: | donaldharvey
Cleanup/optimization | Status: assigned
Component: Documentation | 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 donaldharvey):

* status: new => assigned
* owner: nobody => donaldharvey
* version: 1.6 => master
* easy: 1 => 0


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

Django

unread,
Oct 29, 2015, 7:01:23 PM10/29/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner:
Type: | donaldharvey
Cleanup/optimization | Status: assigned
Component: Documentation | 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:

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

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

Django

unread,
Oct 30, 2015, 4:20:41 AM10/30/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner:
Type: | Status: new
Cleanup/optimization |

Component: Documentation | Version: master
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 claudep):

* owner: donaldharvey =>
* status: assigned => new
* stage: Accepted => Ready for checkin


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

Django

unread,
Oct 30, 2015, 10:24:56 AM10/30/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction of requiring homogeneous
results

-------------------------------------+-------------------------------------
Reporter: pab@… | Owner:
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: master
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
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/21422#comment:6>

Django

unread,
Oct 30, 2015, 10:28:07 AM10/30/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction of requiring homogeneous
results
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner: Tim
Type: | Graham <timograham@…>
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
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 Tim Graham <timograham@…>):

* status: new => closed
* owner: => Tim Graham <timograham@…>
* resolution: => fixed


Comment:

In [changeset:"69239c0f968ee55cb9ca1e31323fb696db0c2aec" 69239c0f]:
{{{
#!CommitTicketReference repository=""
revision="69239c0f968ee55cb9ca1e31323fb696db0c2aec"
Fixed #21422 -- Documented that prefetch_related() results must be
homogeneous.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21422#comment:7>

Django

unread,
Oct 30, 2015, 10:28:31 AM10/30/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction of requiring homogeneous
results
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner: Tim
Type: | Graham <timograham@…>
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
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
-------------------------------------+-------------------------------------

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

In [changeset:"683b3dff7b8f02e5d5a67e678698f420cbd018e9" 683b3df]:
{{{
#!CommitTicketReference repository=""
revision="683b3dff7b8f02e5d5a67e678698f420cbd018e9"
[1.8.x] Fixed #21422 -- Documented that prefetch_related() results must be
homogeneous.

Backport of 69239c0f968ee55cb9ca1e31323fb696db0c2aec from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21422#comment:8>

Django

unread,
Oct 30, 2015, 10:28:32 AM10/30/15
to django-...@googlegroups.com
#21422: prefetch_related does not document restriction of requiring homogeneous
results
-------------------------------------+-------------------------------------
Reporter: pab@… | Owner: Tim
Type: | Graham <timograham@…>
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
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
-------------------------------------+-------------------------------------

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

In [changeset:"31a51a04e899f1fa7fa77be38e3484074d930369" 31a51a04]:
{{{
#!CommitTicketReference repository=""
revision="31a51a04e899f1fa7fa77be38e3484074d930369"
[1.9.x] Fixed #21422 -- Documented that prefetch_related() results must be
homogeneous.

Backport of 69239c0f968ee55cb9ca1e31323fb696db0c2aec from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21422#comment:9>

Reply all
Reply to author
Forward
0 new messages