From a16f13a8661297eda12c4177bb01fa2e5b5ccc56 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 2 Aug 2024 15:21:12 -0400 Subject: Fixed #35643 -- Fixed a crash when ordering a QuerySet by a reference containing "__". Regression in b0ad41198b3e333f57351e3fce5a1fb47f23f376. Refs #34013. The initial logic did not consider that annotation aliases can include lookup or transform separators. Thanks Gert Van Gool for the report and Mariusz Felisiak for the review. --- django/db/models/sql/compiler.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 1d426f49b6..49263d5944 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -403,8 +403,13 @@ class SQLCompiler: ) continue - ref, *transforms = col.split(LOOKUP_SEP) - if expr := self.query.annotations.get(ref): + if expr := self.query.annotations.get(col): + ref = col + transforms = [] + else: + ref, *transforms = col.split(LOOKUP_SEP) + expr = self.query.annotations.get(ref) + if expr: if self.query.combinator and self.select: if transforms: raise NotImplementedError( -- cgit v1.3