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):