1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #34013 -- Added QuerySet.order_by() support for annotation transforms.

Thanks Eugene Morozov and Ben Nace for the reports.
This commit is contained in:
Simon Charette
2023-12-08 02:03:14 -05:00
committed by Mariusz Felisiak
parent fcf95e5927
commit b0ad41198b
6 changed files with 97 additions and 12 deletions

View File

@@ -387,18 +387,24 @@ class SQLCompiler:
True,
)
continue
if col in self.query.annotations:
# References to an expression which is masked out of the SELECT
# clause.
ref, *transforms = col.split(LOOKUP_SEP)
if expr := self.query.annotations.get(ref):
if self.query.combinator and self.select:
if transforms:
raise NotImplementedError(
"Ordering combined queries by transforms is not "
"implemented."
)
# Don't use the resolved annotation because other
# combinated queries might define it differently.
expr = F(col)
else:
expr = self.query.annotations[col]
if isinstance(expr, Value):
# output_field must be resolved for constants.
expr = Cast(expr, expr.output_field)
# combined queries might define it differently.
expr = F(ref)
if transforms:
for name in transforms:
expr = self.query.try_transform(expr, name)
if isinstance(expr, Value):
# output_field must be resolved for constants.
expr = Cast(expr, expr.output_field)
yield OrderBy(expr, descending=descending), False
continue