I've been working on updating the existing code base that was using Django 1.6 to Django 1.8. In the process, I've been facing a particular problem with aggregates. In this code, PGDAggregate 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 PGDAggregate(Aggregate):
"""
Modified to allow Aggregate functions outside of the Django module
"""
def add_to_query(self, query, alias, col, source, is_summary):
"""Add the aggregate to the nominated query.
This method is used to convert the generic Aggregate definition into a
backend-specific definition.
* query is the backend-specific query instance to which the aggregate
is to be added.
* col is a column reference describing the subject field
of the aggregate. It can be an alias, or a tuple describing
a table and column name.
* source is the underlying field or aggregate definition for
the column reference. If the aggregate is not an ordinal or
computed type, this reference is used to determine the coerced
output type of the aggregate.
* is_summary is a boolean that is set True if the aggregate is a
summary value rather than an annotation.
"""
klass = globals()['%sSQL' % self.name]
aggregate = klass(col, source=source, is_summary=is_summary, **self.extra)
# Validate that the backend has a fully supported, correct
# implementation of this aggregate
query.aggregates[alias] = aggregate
self.aggregate = aggregate
class BinSort(PGDAggregate):
alias = 'BinSort'
name = 'BinSort'
class BinSortSQL(SQLAggregate):
sql_function = ''
sql_template = '%(function)sFLOOR((IF(%(field)s<%(offset).16f,360,0)+%(field)s-%(offset).16f)/%(bincount).16f)-IF(%(field)s=%(max).16f,1,0)'
This is how I'm trying to use the aggregate attribute(an instance of Default SQL Aggregate) from the second file to invoke the as_sql method.
sortx = BinSort(xTextString, offset=x, bincount=xbin, max=x1)
sorty = BinSort(yTextString, offset=y, bincount=ybin, max=y1)
annotated_query.annotate(x=sortx, y=sorty)
cn = connections['default']
qn = SQLCompiler(annotated_query.query, cn, 'default').quote_name_unless_alias
sortx_sql = sortx.aggregate.as_sql(qn, cn)[0]
sorty_sql = sorty.aggregate.as_sql(qn, cn)[0]
The error that I'm getting(in l:6) in this implementation is,
exception 'BinSort' object has no attribute 'aggregate'
As steps of debugging, i've tried to check if the BinSort instance has an attribute "aggregate", using
hasattr(sortx, 'aggregate')
which has returned me False. But when I try to check by printing the aggregate attribute from inside the add_to_query method, I could very much see the attribute getting printed. Also, I've implemented this in the way specified in Django 1.8 doc, https://github.com/django/django/blob/stable/1.8.x/django/db/models/aggregates.py#L46