mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #30542 -- Fixed crash of numerical aggregations with filter.
Filters in annotations crashed when used with numerical-type
aggregations (i.e. Avg, StdDev, and Variance). This was caused as the
source expressions no not necessarily have an output_field (such as the
filter field), which lead to an AttributeError: 'WhereNode' object has
no attribute output_field.
Thanks to Chuan-Zheng Lee for the report.
Regression in c690afb873
and two following
commits.
This commit is contained in:
parent
1f81e2df69
commit
4b6dfe1622
3 changed files with 21 additions and 6 deletions
|
@ -1,8 +1,11 @@
|
|||
import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db.models import Case, Count, F, OuterRef, Q, Subquery, Sum, When
|
||||
from django.db.models import (
|
||||
Avg, Case, Count, F, OuterRef, Q, StdDev, Subquery, Sum, Variance, When,
|
||||
)
|
||||
from django.test import TestCase
|
||||
from django.test.utils import Approximate
|
||||
|
||||
from .models import Author, Book, Publisher
|
||||
|
||||
|
@ -40,6 +43,16 @@ class FilteredAggregateTests(TestCase):
|
|||
agg = Sum('age', filter=Q(name__startswith='test'))
|
||||
self.assertEqual(Author.objects.aggregate(age=agg)['age'], 200)
|
||||
|
||||
def test_filtered_numerical_aggregates(self):
|
||||
for aggregate, expected_result in (
|
||||
(Avg, Approximate(66.7, 1)),
|
||||
(StdDev, Approximate(24.9, 1)),
|
||||
(Variance, Approximate(622.2, 1)),
|
||||
):
|
||||
with self.subTest(aggregate=aggregate.__name__):
|
||||
agg = aggregate('age', filter=Q(name__startswith='test'))
|
||||
self.assertEqual(Author.objects.aggregate(age=agg)['age'], expected_result)
|
||||
|
||||
def test_double_filtered_aggregates(self):
|
||||
agg = Sum('age', filter=Q(Q(name='test2') & ~Q(name='test')))
|
||||
self.assertEqual(Author.objects.aggregate(age=agg)['age'], 60)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue