0

I've been facing a particular problem with aggregates. In this code, sumcase class has a method add_to_query which is intended to instantiate the SQL implementation of the aggregate and sets it as a class variable (aggregate) which i'd be using to call the as_sql method in Default SQL Aggregate (django/db.models.sql.aggregates.Aggregate) from another file.

My code (The first file, how I implement aggregate):

from django.db.models.aggregates import Aggregate
from django.db.models.sql.aggregates import Aggregate as SQLAggregate

class SQLsumcase(SQLAggregate):

is_ordinal = True

sql_function = 'SUM'

sql_template = "%(function)s(CASE WHEN %(when)s THEN %(field)s ELSE 0 END)"

def __init__(self, col, **extra):
    if isinstance(extra['when'], basestring):
        extra['when'] = "%s" % extra['when']

    if extra['when'] is None:
        extra['when'] = True

    super(SQLSumCase, self).__init__(col, **extra)


class SumCase(Aggregate):
2
  • What Django version are you using Commented Apr 20, 2018 at 13:39
  • Django 1.11.12 python 2.7.6 Commented Apr 20, 2018 at 13:49

3 Answers 3

1

The aggregates module has been moved to django.db.models as I see in version 1.11.17

Don't know if this is the case you encounter but it may help.

Sign up to request clarification or add additional context in comments.

Comments

0

There is not a module named aggregates in django.db.models.sql also Aggregate is not a class on this sql module. I think you should use:

from django.db.models.sql import AggregateQuery

Comments

0

django.db.models.sql.aggregates module is removed from 1.10 version.

1 Comment

While this may theoretically answer the question, it could be suggested as a comment only. Maybe you can expand the answer a bit by clicking edit and tell from which version onwards this module is removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.