1
0
mirror of https://github.com/django/django.git synced 2025-03-12 18:30:48 +00:00

Refs #10929 -- Fixed aggregates crash when passing strings as defaults.

Previously strings were interpreted as F() expressions and default
crashed with AttributeError:
    'F' object has no attribute 'empty_result_set_value'
This commit is contained in:
Simon Charette 2022-11-29 12:31:14 +01:00 committed by Mariusz Felisiak
parent 85b52d22fd
commit 0db8bf3d60
2 changed files with 4 additions and 1 deletions

View File

@ -2,7 +2,7 @@
Classes to represent the definitions of aggregate functions. Classes to represent the definitions of aggregate functions.
""" """
from django.core.exceptions import FieldError, FullResultSet from django.core.exceptions import FieldError, FullResultSet
from django.db.models.expressions import Case, Func, Star, When from django.db.models.expressions import Case, Func, Star, Value, When
from django.db.models.fields import IntegerField from django.db.models.fields import IntegerField
from django.db.models.functions.comparison import Coalesce from django.db.models.functions.comparison import Coalesce
from django.db.models.functions.mixins import ( from django.db.models.functions.mixins import (
@ -85,6 +85,8 @@ class Aggregate(Func):
return c return c
if hasattr(default, "resolve_expression"): if hasattr(default, "resolve_expression"):
default = default.resolve_expression(query, allow_joins, reuse, summarize) default = default.resolve_expression(query, allow_joins, reuse, summarize)
else:
default = Value(default, c._output_field_or_none)
c.default = None # Reset the default argument before wrapping. c.default = None # Reset the default argument before wrapping.
coalesce = Coalesce(c, default, output_field=c._output_field_or_none) coalesce = Coalesce(c, default, output_field=c._output_field_or_none)
coalesce.is_summary = c.is_summary coalesce.is_summary = c.is_summary

View File

@ -125,6 +125,7 @@ class TestGeneralAggregate(PostgreSQLTestCase):
(BoolAnd("boolean_field", default=False), False), (BoolAnd("boolean_field", default=False), False),
(BoolOr("boolean_field", default=False), False), (BoolOr("boolean_field", default=False), False),
(JSONBAgg("integer_field", default=Value('["<empty>"]')), ["<empty>"]), (JSONBAgg("integer_field", default=Value('["<empty>"]')), ["<empty>"]),
(StringAgg("char_field", delimiter=";", default="<empty>"), "<empty>"),
( (
StringAgg("char_field", delimiter=";", default=Value("<empty>")), StringAgg("char_field", delimiter=";", default=Value("<empty>")),
"<empty>", "<empty>",