#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.