﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37274	Transforms in order_by() not allowed after alias()	Jacob Walls		"This example fails with:
{{{
django.core.exceptions.FieldError: Cannot resolve keyword 'other_pubdate' into field. Choices are: ... other_pubdate, ...
}}}
{{{#!diff
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index 42fccca7d6..4baef9e463 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -1481,6 +1481,13 @@ class AliasTests(TestCase):
         self.assertIs(hasattr(qs.first(), ""other_age""), False)
         self.assertQuerySetEqual(qs, [34, 34, 35, 46, 57], lambda a: a.age)
 
+    def test_order_by_alias_transform(self):
+        qs = (
+            Book.objects.alias(other_pubdate=F(""pubdate""))
+            .order_by(""-other_pubdate__year"")
+        )
+        self.assertQuerySetEqual(qs, [2008, 2007, 1995, 1991], lambda a: a.pubdate.year)
+
     def test_order_by_alias_aggregate(self):
         qs = (
             Author.objects.values(""age"")
}}}

However, all of these variants work, suggesting to me that there should be a simple fix.

Keep the transform, change alias to annotate:
{{{#!py
Book.objects.annotate(other_pubdate=F(""pubdate"")).order_by(""other_pubdate__year"")
}}}
Keep the transform, use a field name:
{{{#!py
Book.objects.order_by(""pubdate__year"")
}}}
Keep the alias, drop the transform:
{{{#!py
Book.objects.alias(other_pubdate=F(""pubdate"")).order_by(""other_pubdate"")
}}}

If not practical for some reason, then at the very least we should improve ""Cannot resolve keyword 'other_pubdate' into field. Choices are: ... other_pubdate, ...""."	Bug	new	Database layer (models, ORM)	6.1	Normal			Pravin	Unreviewed	0	0	0	0	0	0
