summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/sql/compiler.py9
-rw-r--r--tests/aggregation/tests.py20
2 files changed, 27 insertions, 2 deletions
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(
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 075e707102..a5914f1878 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -1750,6 +1750,26 @@ class AggregateTestCase(TestCase):
],
)
+ def test_order_by_aggregate_default_alias(self):
+ publisher_books = (
+ Publisher.objects.values("book")
+ .annotate(Count("book"))
+ .order_by("book__count", "book__id")
+ .values_list("book", flat=True)
+ )
+ self.assertQuerySetEqual(
+ publisher_books,
+ [
+ None,
+ self.b1.id,
+ self.b2.id,
+ self.b3.id,
+ self.b4.id,
+ self.b5.id,
+ self.b6.id,
+ ],
+ )
+
def test_empty_result_optimization(self):
with self.assertNumQueries(0):
self.assertEqual(