@@ -177,24 +177,31 @@ def _fix_aggregates(self):
177
177
match behavior of other django backends, it needs to not drop remainders.
178
178
E.g. AVG([1, 2]) needs to yield 1.5, not 1
179
179
"""
180
- for alias , aggregate in self .query .aggregate_select .items ():
180
+ try :
181
+ # for django 1.10 and up (works starting in 1.8 so I am told)
182
+ select = self .query .annotation_select
183
+ except AttributeError :
184
+ # older
185
+ select = self .query .aggregate_select
186
+
187
+ for alias , aggregate in select .items ():
181
188
if not hasattr (aggregate , 'sql_function' ):
182
189
continue
183
190
if aggregate .sql_function == 'AVG' :# and self.connection.cast_avg_to_float:
184
191
# Embed the CAST in the template on this query to
185
192
# maintain multi-db support.
186
- self . query . aggregate_select [alias ].sql_template = \
193
+ select [alias ].sql_template = \
187
194
'%(function)s(CAST(%(field)s AS FLOAT))'
188
195
# translate StdDev function names
189
196
elif aggregate .sql_function == 'STDDEV_SAMP' :
190
- self . query . aggregate_select [alias ].sql_function = 'STDEV'
197
+ select [alias ].sql_function = 'STDEV'
191
198
elif aggregate .sql_function == 'STDDEV_POP' :
192
- self . query . aggregate_select [alias ].sql_function = 'STDEVP'
199
+ select [alias ].sql_function = 'STDEVP'
193
200
# translate Variance function names
194
201
elif aggregate .sql_function == 'VAR_SAMP' :
195
- self . query . aggregate_select [alias ].sql_function = 'VAR'
202
+ select [alias ].sql_function = 'VAR'
196
203
elif aggregate .sql_function == 'VAR_POP' :
197
- self . query . aggregate_select [alias ].sql_function = 'VARP'
204
+ select [alias ].sql_function = 'VARP'
198
205
199
206
def as_sql (self , with_limits = True , with_col_aliases = False ):
200
207
# Django #12192 - Don't execute any DB query when QS slicing results in limit 0
0 commit comments