﻿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
37350	"distinct(*fields) does not fathom aliases containing the lookup separator: ""__"""	Jacob Walls	Django Sprints	"We are gradually improving the situation with respect to the treatment of annotation aliases containing the lookup separator, see [https://github.com/django/django/pull/21803#discussion_r3823261022 PR discussion]. This ticket is for `.distinct(*fields)`.

When an alias is provided to `.distinct(*fields)`, is it split on `LOOKUP_SEP` before checking against the alias map, meaning it will miss an alias that contains it.

Reproducer:
{{{#!py
qs = Person.objects.annotate(my__val=models.Value(1)).distinct(""my__val"")
print(qs)
}}}
{{{
  File ""/django-ref/django/db/models/sql/query.py"", line 1854, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'my' into field. Choices are: id, my__val, name
}}}


The helper from 73cc09f14f13fedddc14d6ba5b287cb33c24e4a4 could be used as part of fixing this, so that we don't omit any nuance with `FilteredRelation`.

Using the lookup separator in an annotation alias sounds a little unorthodox, but it's present in the [https://github.com/django/django/blob/9cf9c796be8dd53bc3b11355ff39d65c81d7be6d/django/db/models/aggregates.py#L111 default alias] generated by `.aggregate()`, and we've taken reports on it before. Also, if you let users choose aliases in configurable reports, it's bound to come up, and we've taken issues on that.
"	Bug	assigned	Database layer (models, ORM)	dev	Normal				Unreviewed	0	0	0	0	0	0
