#32811: Annotate removes Meta.ordering
-------------------------------------+-------------------------------------
     Reporter:  pirelle              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Peter Law):

 Request that this be reconsidered.

 My use-case was similar to the one mentioned here, however I was loading
 full models (with a similar count annotation) and not any form of
 `.values()`. Given the history it seems likely that that was never the
 intention.

 I can see that this was previously deprecated and may now be expected,
 however it does not appear to be documented anywhere and is very
 surprising.

 Here's where I've (since) checked for docs relating to this:
 - https://docs.djangoproject.com/en/3.2/ref/models/options/#ordering
 - https://docs.djangoproject.com/en/3.2/topics/db/aggregation/
 - https://docs.djangoproject.com/en/3.2/ref/models/expressions/#aggregate-
 expressions

 What I've seen of the reasoning for the deprecation appears to be related
 specifically to `.values()` queries, and then really to cases where that
 is used to create a grouping ''of the main queried table'' (where the user
 is probably aware that that's what they're doing). However the behaviour
 change impacts ''all'' querysets (including those which return full
 models) and the behaviour here is ''not'' intending to create a grouping
 of things on the main table, but rather of a related table for the
 purposes of counting (where the user is unlikely to be aware that a group-
 by is the result).

 Perhaps the core issue here is that the ORM doesn't have an explicit
 "group by" mechanism and so is having to infer this by pattern matching
 the structure of the `QuerySet`, leading to the issues like the original
 one where default ordering is applied in ways that produce potentially
 unexpected results.

 At the very least I think this (current behaviour) needs to be clearly
 documented as a part of both the `Meta.ordering` and aggregation docs,
 however to be honest I would prefer the previous behaviour. It feels very
 surprising that Django will silently drop part of its model configuration
 in some cases!
 At least if this were documented people could see what those cases were,
 however at the moment the only way to know is when you're bitten by the
 issue described here.

 Aside: the original ticket (https://code.djangoproject.com/ticket/10574)
 appears to suggest that part of the motivation is that in queries such as
 `Model.objects.values('data')` applying the default ordering is redundant,
 which I'd strongly argue against -- having values-only results ordered by
 something (even a field not loaded) seems entirely useful to me.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.f80c5fda1ee68462dcb0113832ec706c%40djangoproject.com.

Reply via email to