#36686 closed Cleanup/optimization (fixed)
Clarify the behavior difference between order_by() and Meta.ordering in GROUP BY queries
| Reported by: | Wouter | Owned by: | Varun Kasyap Pentamaraju |
|---|---|---|---|
| Component: | Documentation | Version: | 5.2 |
| Severity: | Normal | Keywords: | aggregate, order_by, ordering |
| Cc: | Varun Kasyap Pentamaraju | 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
Today I hunted an ordering bug. I have a model with ordering = ['name']
And in a view I use:
queryset.annotate(used_by=Count('somerelation')) which drops the order_by.
However the documentation for a aggregation states: https://docs.djangoproject.com/en/5.2/topics/db/aggregation/ that:
Django never removes ordering constraints that you have specified.
But then I saw the changelog for version. 2.2. Which explicitly states:
A model’s Meta.ordering affecting GROUP BY queries (such as .annotate().values()) is a common source of confusion. Such queries now issue a deprecation warning with the advice to add an order_by() to retain the current query. Meta.ordering will be ignored in such queries starting in Django 3.1.
Where do I find this information in the current documentation? I think this information is currently missing.
Change History (17)
comment:1 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 3 weeks ago
| Keywords: | aggregate order_by ordering added |
|---|---|
| Summary: | Update documentation on 'never removes' ordering. → Clarify the behavior difference between order_by() and Meta.ordering in GROUP BY queries |
comment:3 by , 3 weeks ago
I just happened to bump into this additional language:
Similarly, if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.
Might want to clarify that one also?
comment:4 by , 3 weeks ago
| Type: | Bug → Cleanup/optimization |
|---|
comment:5 by , 3 weeks ago
| Cc: | added |
|---|---|
| Owner: | set to |
| Status: | new → assigned |
I would like to work on this and raise PR, Thanks
comment:7 by , 2 weeks ago
The patches look okay to me.
What still is difficult to grasp is why order_by 'always' works if applied explicitly, but using ordering does not.
comment:8 by , 2 weeks ago
| Patch needs improvement: | set |
|---|
comment:9 by , 2 weeks ago
| Patch needs improvement: | unset |
|---|
1)Changed from "or" to "and"
2)Used admonition instead of note
3)Better English
comment:10 by , 2 weeks ago
| Patch needs improvement: | set |
|---|
comment:11 by , 2 weeks ago
| Patch needs improvement: | unset |
|---|
1) reversed order of values() and annotate()
2) reverted querysets.txt
comment:12 by , 2 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:13 by , 41 hours ago
| Patch needs improvement: | set |
|---|---|
| Triage Stage: | Ready for checkin → Accepted |
comment:14 by , 40 hours ago
| Patch needs improvement: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
The current documentation only discusses the effect of performing a
GROUP BYquery after a queryset with.order_by(), but I agree that from this you cannot deduce the effect of doing the same on a model withMeta.ordering, which you might assume acts the same (especially given the "Django never..." wording you quote).Clarifying that note would be welcome. Would you like to submit a PR?