[Django] #37308: Lookups on annotation aliases in .values() and .order_by() resolve unexpectedly

3 views
Skip to first unread message

Django

unread,
Aug 31, 2026, 4:51:27 AM (23 hours ago) Aug 31
to django-...@googlegroups.com
#37308: Lookups on annotation aliases in .values() and .order_by() resolve
unexpectedly
-------------------------------------+-------------------------------------
Reporter: Annabelle Wiegart | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 6.1 | Severity: Normal
Keywords: alias, values, | Triage Stage:
order_by | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Doing a lookup on an annotation alias results in an unexpected name
resolution. When calling e.g. `.values(alias + "__pk")`,
Query.names_to_path() checks if `f"{alias}__pk"` is the name of an
annotation. Otherwise it tries to resolve the name into field plus lookup.
This might be unexpected. While allowing lookups on aliases may add too
much complexity and therefore not be desirable, it would probably be
helpful to at least show a warning when `.values()` or `.order_by()` is
called on an expression containing an alias name and `__`.

The issue has been discussed in
[https://github.com/django/django/pull/21803#discussion_r3816836443
PR21803] for #36945.

[https://dryorm.xterm.info/lookup-on-alias ORM fiddle]

Reproducer:

{{{#!div style="font-size: 80%"
{{{#!python
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType


class Person(models.Model):
name = models.CharField(max_length=100)
creator = models.ForeignKey(User, models.CASCADE, null=True)
ct = models.ForeignKey(ContentType, models.CASCADE)


def run():
admin = User.objects.create(username='admin')
person_ct = ContentType.objects.get_for_model(Person)
Person.objects.create(creator=admin, name="Claude", ct=person_ct)
# resolves correctly
qs1 = Person.objects.all().values("creator__pk")
print(qs1)

alias = "myalias"
# raises FieldError
qs2 = Person.objects.annotate(**{alias:
models.F("creator")}).values(alias + "__pk")
print(qs2)
# raises FieldError
qs3 = Person.objects.annotate(
**{alias: models.FilteredRelation("creator",
condition=models.Q(creator__isnull=False))}
).values(alias + "__pk")
print(qs3)
}}}
}}}

Output:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword 'myalias' into
field. Choices are: creator, creator_id, ct, ct_id, id, name
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/37308>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 31, 2026, 4:53:23 AM (23 hours ago) Aug 31
to django-...@googlegroups.com
#37308: Lookups on annotation aliases in .values() and .order_by() resolve
unexpectedly
-------------------------------------+-------------------------------------
Reporter: Annabelle Wiegart | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 6.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: alias, values, | Triage Stage:
order_by | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Annabelle Wiegart):

* type: Uncategorized => Cleanup/optimization

--
Ticket URL: <https://code.djangoproject.com/ticket/37308#comment:1>

Django

unread,
Aug 31, 2026, 8:21:55 AM (20 hours ago) Aug 31
to django-...@googlegroups.com
#37308: Lookups on annotation aliases in .values() and .order_by() resolve
unexpectedly
-------------------------------------+-------------------------------------
Reporter: Annabelle Wiegart | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 6.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: alias, values, | Triage Stage: Accepted
order_by |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Unreviewed => Accepted

Comment:

Maybe we should retry by splitting on the separator from right to left,
e.g. for `a__b__c`, try `a__b__c`, then `a__b` + `c` (lookup), then `a` +
`b__c` (transform/lookup).
--
Ticket URL: <https://code.djangoproject.com/ticket/37308#comment:2>

Django

unread,
1:12 AM (3 hours ago) 1:12 AM
to django-...@googlegroups.com
#37308: Lookups on annotation aliases in .values() and .order_by() resolve
unexpectedly
-------------------------------------+-------------------------------------
Reporter: Annabelle Wiegart | Owner: Zubair
Type: | Hassan
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 6.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: alias, values, | Triage Stage: Accepted
order_by |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Zubair Hassan):

* owner: (none) => Zubair Hassan
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/37308#comment:3>
Reply all
Reply to author
Forward
0 new messages