summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-03-25 15:22:45 -0400
committerGitHub <noreply@github.com>2023-03-25 20:22:45 +0100
commitcb13792938f2c887134eb6b5164d89f8d8f9f1bd (patch)
tree508a2d0769df6a81699a4121fe07379fff27fbf6 /django
parentf5c5c571d3b87a78d005ea6f21959388d1747696 (diff)
downloaddjango-cb13792938f2c887134eb6b5164d89f8d8f9f1bd.tar.gz
Fixed #34437 -- Made values() resolving error mention selected annotations.
While the add_fields() call from set_values() does trigger validation it does so after annotations are masked resulting in them being excluded from the choices of valid options surfaced through a FieldError.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/sql/query.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 9865e88fd0..3cb8bdbc8a 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -2135,11 +2135,6 @@ class Query(BaseExpression):
# For lookups spanning over relationships, show the error
# from the model on which the lookup failed.
raise
- elif name in self.annotations:
- raise FieldError(
- "Cannot select the '%s' alias. Use annotate() to promote "
- "it." % name
- )
else:
names = sorted(
[
@@ -2385,7 +2380,17 @@ class Query(BaseExpression):
extra_names.append(f)
elif f in self.annotation_select:
annotation_names.append(f)
+ elif f in self.annotations:
+ raise FieldError(
+ f"Cannot select the '{f}' alias. Use annotate() to "
+ "promote it."
+ )
else:
+ # Call `names_to_path` to ensure a FieldError including
+ # annotations about to be masked as valid choices if
+ # `f` is not resolvable.
+ if self.annotation_select:
+ self.names_to_path(f.split(LOOKUP_SEP), self.model._meta)
field_names.append(f)
self.set_extra_mask(extra_names)
self.set_annotation_mask(annotation_names)