Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf6ec4f

Browse files
BenBen
authored andcommitted
In _fix_aggregates, use annotation_select (newer) if available else aggregate_select
1 parent 04104ec commit bf6ec4f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

django_pyodbc/compiler.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,31 @@ def _fix_aggregates(self):
177177
match behavior of other django backends, it needs to not drop remainders.
178178
E.g. AVG([1, 2]) needs to yield 1.5, not 1
179179
"""
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():
181188
if not hasattr(aggregate, 'sql_function'):
182189
continue
183190
if aggregate.sql_function == 'AVG':# and self.connection.cast_avg_to_float:
184191
# Embed the CAST in the template on this query to
185192
# maintain multi-db support.
186-
self.query.aggregate_select[alias].sql_template = \
193+
select[alias].sql_template = \
187194
'%(function)s(CAST(%(field)s AS FLOAT))'
188195
# translate StdDev function names
189196
elif aggregate.sql_function == 'STDDEV_SAMP':
190-
self.query.aggregate_select[alias].sql_function = 'STDEV'
197+
select[alias].sql_function = 'STDEV'
191198
elif aggregate.sql_function == 'STDDEV_POP':
192-
self.query.aggregate_select[alias].sql_function = 'STDEVP'
199+
select[alias].sql_function = 'STDEVP'
193200
# translate Variance function names
194201
elif aggregate.sql_function == 'VAR_SAMP':
195-
self.query.aggregate_select[alias].sql_function = 'VAR'
202+
select[alias].sql_function = 'VAR'
196203
elif aggregate.sql_function == 'VAR_POP':
197-
self.query.aggregate_select[alias].sql_function = 'VARP'
204+
select[alias].sql_function = 'VARP'
198205

199206
def as_sql(self, with_limits=True, with_col_aliases=False):
200207
# Django #12192 - Don't execute any DB query when QS slicing results in limit 0

0 commit comments

Comments
 (0)