File tree Expand file tree Collapse file tree 3 files changed +14
-1
lines changed Expand file tree Collapse file tree 3 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -857,6 +857,9 @@ class ExpressionWrapper(Expression):
857
857
858
858
def __init__ (self , expression , output_field ):
859
859
super ().__init__ (output_field = output_field )
860
+ if getattr (expression , '_output_field_or_none' , True ) is None :
861
+ expression = expression .copy ()
862
+ expression .output_field = output_field
860
863
self .expression = expression
861
864
862
865
def set_source_expressions (self , exprs ):
Original file line number Diff line number Diff line change @@ -170,6 +170,14 @@ def test_annotate_with_aggregation(self):
170
170
self .assertEqual (book .is_book , 1 )
171
171
self .assertEqual (book .rating_count , 1 )
172
172
173
+ def test_combined_expression_annotation_with_aggregation (self ):
174
+ book = Book .objects .annotate (
175
+ combined = ExpressionWrapper (Value (3 ) * Value (4 ), output_field = IntegerField ()),
176
+ rating_count = Count ('rating' ),
177
+ ).first ()
178
+ self .assertEqual (book .combined , 12 )
179
+ self .assertEqual (book .rating_count , 1 )
180
+
173
181
def test_aggregate_over_annotation (self ):
174
182
agg = Author .objects .annotate (other_age = F ('age' )).aggregate (otherage_sum = Sum ('other_age' ))
175
183
other_agg = Author .objects .aggregate (age_sum = Sum ('age' ))
Original file line number Diff line number Diff line change @@ -1837,4 +1837,6 @@ def test_empty_group_by(self):
1837
1837
1838
1838
def test_non_empty_group_by (self ):
1839
1839
expr = ExpressionWrapper (Lower (Value ('f' )), output_field = IntegerField ())
1840
- self .assertEqual (expr .get_group_by_cols (alias = None ), [expr .expression ])
1840
+ group_by_cols = expr .get_group_by_cols (alias = None )
1841
+ self .assertEqual (group_by_cols , [expr .expression ])
1842
+ self .assertEqual (group_by_cols [0 ].output_field , expr .output_field )
You can’t perform that action at this time.
0 commit comments