Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#21422 closed Cleanup/optimization (fixed)

prefetch_related does not document restriction of requiring homogeneous results

Reported by: pab@… Owned by: Tim Graham <timograham@…>
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: loic@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (9)

comment:1 by loic84, 11 years ago

Cc: loic@… added
Triage Stage: UnreviewedAccepted

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

This limitation should be documented.

comment:2 by James Bennett, 9 years ago

Easy pickings: set

comment:3 by Donald Harvey, 9 years ago

Easy pickings: unset
Owner: changed from nobody to Donald Harvey
Status: newassigned
Version: 1.6master

comment:4 by Tim Graham, 9 years ago

Has patch: set

comment:5 by Claude Paroz, 9 years ago

Owner: Donald Harvey removed
Status: assignednew
Triage Stage: AcceptedReady for checkin

comment:6 by Tim Graham, 9 years ago

Summary: prefetch_related does not document restrictionprefetch_related does not document restriction of requiring homogeneous results

comment:7 by Tim Graham <timograham@…>, 9 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 69239c0f:

Fixed #21422 -- Documented that prefetch_related() results must be homogeneous.

comment:8 by Tim Graham <timograham@…>, 9 years ago

In 683b3df:

[1.8.x] Fixed #21422 -- Documented that prefetch_related() results must be homogeneous.

Backport of 69239c0f968ee55cb9ca1e31323fb696db0c2aec from master

comment:9 by Tim Graham <timograham@…>, 9 years ago

In 31a51a04:

[1.9.x] Fixed #21422 -- Documented that prefetch_related() results must be homogeneous.

Backport of 69239c0f968ee55cb9ca1e31323fb696db0c2aec from master

Note: See TracTickets for help on using tickets.
Back to Top