mirror of https://github.com/django/django.git
Refs #27849 -- Fixed filtered aggregates crash on filters that match everything.
This commit is contained in:
parent
77cf70ea96
commit
967f8750ab
|
@ -105,17 +105,18 @@ class Aggregate(Func):
|
||||||
if self.filter:
|
if self.filter:
|
||||||
if connection.features.supports_aggregate_filter_clause:
|
if connection.features.supports_aggregate_filter_clause:
|
||||||
filter_sql, filter_params = self.filter.as_sql(compiler, connection)
|
filter_sql, filter_params = self.filter.as_sql(compiler, connection)
|
||||||
template = self.filter_template % extra_context.get(
|
if filter_sql:
|
||||||
"template", self.template
|
template = self.filter_template % extra_context.get(
|
||||||
)
|
"template", self.template
|
||||||
sql, params = super().as_sql(
|
)
|
||||||
compiler,
|
sql, params = super().as_sql(
|
||||||
connection,
|
compiler,
|
||||||
template=template,
|
connection,
|
||||||
filter=filter_sql,
|
template=template,
|
||||||
**extra_context,
|
filter=filter_sql,
|
||||||
)
|
**extra_context,
|
||||||
return sql, (*params, *filter_params)
|
)
|
||||||
|
return sql, (*params, *filter_params)
|
||||||
else:
|
else:
|
||||||
copy = self.copy()
|
copy = self.copy()
|
||||||
copy.filter = None
|
copy.filter = None
|
||||||
|
|
|
@ -205,3 +205,16 @@ class FilteredAggregateTests(TestCase):
|
||||||
max_rating=Max("rating", filter=Q(rating__in=[]))
|
max_rating=Max("rating", filter=Q(rating__in=[]))
|
||||||
)
|
)
|
||||||
self.assertEqual(aggregate, {"max_rating": None})
|
self.assertEqual(aggregate, {"max_rating": None})
|
||||||
|
|
||||||
|
def test_filtered_aggregate_full_condition(self):
|
||||||
|
book = Book.objects.annotate(
|
||||||
|
authors_count=Count(
|
||||||
|
"authors",
|
||||||
|
filter=~Q(authors__in=[]),
|
||||||
|
),
|
||||||
|
).get(pk=self.b1.pk)
|
||||||
|
self.assertEqual(book.authors_count, 2)
|
||||||
|
aggregate = Book.objects.aggregate(
|
||||||
|
max_rating=Max("rating", filter=~Q(rating__in=[]))
|
||||||
|
)
|
||||||
|
self.assertEqual(aggregate, {"max_rating": 4.5})
|
||||||
|
|
Loading…
Reference in New Issue