mirror of
https://github.com/django/django.git
synced 2025-06-04 02:59:13 +00:00
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.
This commit is contained in:
parent
f5c5c571d3
commit
cb13792938
@ -2135,11 +2135,6 @@ class Query(BaseExpression):
|
|||||||
# For lookups spanning over relationships, show the error
|
# For lookups spanning over relationships, show the error
|
||||||
# from the model on which the lookup failed.
|
# from the model on which the lookup failed.
|
||||||
raise
|
raise
|
||||||
elif name in self.annotations:
|
|
||||||
raise FieldError(
|
|
||||||
"Cannot select the '%s' alias. Use annotate() to promote "
|
|
||||||
"it." % name
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
names = sorted(
|
names = sorted(
|
||||||
[
|
[
|
||||||
@ -2385,7 +2380,17 @@ class Query(BaseExpression):
|
|||||||
extra_names.append(f)
|
extra_names.append(f)
|
||||||
elif f in self.annotation_select:
|
elif f in self.annotation_select:
|
||||||
annotation_names.append(f)
|
annotation_names.append(f)
|
||||||
|
elif f in self.annotations:
|
||||||
|
raise FieldError(
|
||||||
|
f"Cannot select the '{f}' alias. Use annotate() to "
|
||||||
|
"promote it."
|
||||||
|
)
|
||||||
else:
|
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)
|
field_names.append(f)
|
||||||
self.set_extra_mask(extra_names)
|
self.set_extra_mask(extra_names)
|
||||||
self.set_annotation_mask(annotation_names)
|
self.set_annotation_mask(annotation_names)
|
||||||
|
@ -33,6 +33,7 @@ from django.db.models.functions import (
|
|||||||
Lower,
|
Lower,
|
||||||
Trim,
|
Trim,
|
||||||
)
|
)
|
||||||
|
from django.db.models.sql.query import get_field_names_from_opts
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
from django.test.utils import register_lookup
|
from django.test.utils import register_lookup
|
||||||
|
|
||||||
@ -465,6 +466,16 @@ class NonAggregateAnnotationTestCase(TestCase):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_values_wrong_annotation(self):
|
||||||
|
expected_message = (
|
||||||
|
"Cannot resolve keyword 'annotation_typo' into field. Choices are: %s"
|
||||||
|
)
|
||||||
|
article_fields = ", ".join(
|
||||||
|
["annotation"] + sorted(get_field_names_from_opts(Book._meta))
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(FieldError, expected_message % article_fields):
|
||||||
|
Book.objects.annotate(annotation=Value(1)).values_list("annotation_typo")
|
||||||
|
|
||||||
def test_decimal_annotation(self):
|
def test_decimal_annotation(self):
|
||||||
salary = Decimal(10) ** -Employee._meta.get_field("salary").decimal_places
|
salary = Decimal(10) ** -Employee._meta.get_field("salary").decimal_places
|
||||||
Employee.objects.create(
|
Employee.objects.create(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user