From df32fd42b84cc6dbba173201f244491b0d154a63 Mon Sep 17 00:00:00 2001 From: Thodoris Sotiropoulos Date: Wed, 3 Jun 2020 00:14:01 +0300 Subject: [PATCH] Fixed #31651 -- Made ExpressionWrapper use grouping columns from wrapped expression. --- django/db/models/expressions.py | 3 +++ tests/expressions/tests.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index c1a76584f0..6bd1471692 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -863,6 +863,9 @@ class ExpressionWrapper(Expression): def get_source_expressions(self): return [self.expression] + def get_group_by_cols(self, alias=None): + return self.expression.get_group_by_cols(alias=alias) + def as_sql(self, compiler, connection): return self.expression.as_sql(compiler, connection) diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 72bca4f205..57e4c8ebc7 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1828,3 +1828,13 @@ class CombinableTests(SimpleTestCase): def test_reversed_or(self): with self.assertRaisesMessage(NotImplementedError, self.bitwise_msg): object() | Combinable() + + +class ExpressionWrapperTests(SimpleTestCase): + def test_empty_group_by(self): + expr = ExpressionWrapper(Value(3), output_field=IntegerField()) + self.assertEqual(expr.get_group_by_cols(alias=None), []) + + def test_non_empty_group_by(self): + expr = ExpressionWrapper(Lower(Value('f')), output_field=IntegerField()) + self.assertEqual(expr.get_group_by_cols(alias=None), [expr.expression])