Opened 3 months ago
Last modified 12 days ago
#36507 assigned Cleanup/optimization
Document that only the base queryset’s prefetch_related() is preserved when using union()
Reported by: | Clifford Gama | Owned by: | Chris M |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.2 |
Severity: | Normal | Keywords: | prefetch_related, union |
Cc: | Sniom | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
When using QuerySet.union() (possibly also intersection()
and difference()
) in Django, any prefetch_related()
applied to the second (and third, etc) queryset is silently ignored. See forum thread in which it was suggested to open a documentation ticket. A docs improvement should also likely include the workaround for using Q
objects to do all prefetches in the base qs (where possible), which is suggested by Simon in the forum.
Change History (5)
comment:1 by , 3 months ago
Description: | modified (diff) |
---|
comment:2 by , 3 months ago
Cc: | added |
---|
comment:3 by , 3 months ago
Component: | Documentation → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 3 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:5 by , 12 days ago
Has patch: | set |
---|---|
Owner: | changed from | to
I have made an attempt at documenting this behavior in this PR.
I'm open to thoughts on how to improve this documentation: if it should exist somewhere else or if any changes are needed. It is a pretty nuanced topic, and I was trying to balance covering the details without making it too confusing.
I included an example of using Q
objects to filter within the first prefetch_related
. However, I'm not sure this is all that helpful of an example to provide. It feels like it introduces a bit too much complexity to the documentation.
Makes sense, thank you Clifford!