1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #36155 -- Improved error handling when annotate arguments require an alias.

Regression in ed0cbc8d8b.
This commit is contained in:
Vinko Mlačić
2025-01-28 22:57:32 +01:00
committed by Sarah Boyce
parent b84478ae95
commit c6ace896a2
3 changed files with 23 additions and 2 deletions

View File

@@ -969,6 +969,24 @@ class NonAggregateAnnotationTestCase(TestCase):
):
Book.objects.annotate(BooleanField(), Value(False), is_book=True)
def test_complex_annotations_must_have_an_alias(self):
complex_annotations = [
F("rating") * F("price"),
Value("title"),
Case(When(pages__gte=400, then=Value("Long")), default=Value("Short")),
Subquery(
Book.objects.filter(publisher_id=OuterRef("pk"))
.order_by("-pubdate")
.values("name")[:1]
),
Exists(Book.objects.filter(publisher_id=OuterRef("pk"))),
]
msg = "Complex annotations require an alias"
for annotation in complex_annotations:
with self.subTest(annotation=annotation):
with self.assertRaisesMessage(TypeError, msg):
Book.objects.annotate(annotation)
def test_chaining_annotation_filter_with_m2m(self):
qs = (
Author.objects.filter(