django-pyodbc icon indicating copy to clipboard operation
django-pyodbc copied to clipboard

QuerySet.count() method is broken on Django 1.8

Open dan-passaro opened this issue 9 years ago • 3 comments

I get this traceback when trying to execute a Model.objects.count() with django-pyodbc 0.3.0 and Django 1.8.4.

[...]/local/lib/python2.7/site-packages/django_pyodbc/compiler.pyc in _fix_aggregates(self)
    116         """
    117         for alias, aggregate in self.query.aggregate_select.items():
--> 118             if aggregate.sql_function == 'AVG':# and self.connection.cast_avg_to_float:
    119                 # Embed the CAST in the template on this query to
    120                 # maintain multi-db support.

AttributeError: 'Count' object has no attribute 'sql_function'

I do not get this same error when using Django 1.7.10.

dan-passaro avatar Oct 01 '15 18:10 dan-passaro

Same issue with other aggreagate functions like Max and Min

You can patch it by setting it to this:

if type(aggregate) == django.db.models.aggregates.Max: #.sql_function == 'AVG':

But thte aggregates construct doesn't have enough types to map to all the handlings in MSSQL.

dir(django.db.models.aggregates) ['Aggregate', 'Avg', 'Count', 'FieldError', 'FloatField', 'Func', 'IntegerField', 'Max', 'Min', 'StdDev', 'Sum', 'Value', 'Variance', 'all', 'builtins', 'doc', 'file', 'name', 'package']

mwatchi avatar Dec 24 '15 18:12 mwatchi

Bump

fizxmike avatar Feb 05 '16 16:02 fizxmike

For whom it may help, we switched to django-pyodbc-azure. It is kept much more up to date than this lib and is not tied to Azure in any special way, despite the name. Highly recommended 👍

dan-passaro avatar Feb 05 '16 19:02 dan-passaro