From aff79be03a8c787ba0cc7e00dcec9eeb8ad01c87 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 31 Jan 2022 11:33:24 +0100 Subject: [4.0.x] Fixed #33468 -- Fixed QuerySet.aggregate() after annotate() crash on aggregates with default. Thanks Adam Johnson for the report. Backport of 71e7c8e73712419626f1c2b6ec036e8559a2d667 from main --- tests/aggregation/tests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/aggregation') diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 72575a526c..68d3c4d31e 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -1604,6 +1604,18 @@ class AggregateTestCase(TestCase): ) self.assertAlmostEqual(result['value'], Decimal('61.72'), places=2) + def test_aggregation_default_after_annotation(self): + result = Publisher.objects.annotate( + double_num_awards=F('num_awards') * 2, + ).aggregate(value=Sum('double_num_awards', default=0)) + self.assertEqual(result['value'], 40) + + def test_aggregation_default_not_in_aggregate(self): + result = Publisher.objects.annotate( + avg_rating=Avg('book__rating', default=2.5), + ).aggregate(Sum('num_awards')) + self.assertEqual(result['num_awards__sum'], 20) + def test_exists_none_with_aggregate(self): qs = Book.objects.all().annotate( count=Count('id'), -- cgit v1.3