我一直在努力更新使用Django 1.6到Django 1.8的现有代码库。在这个过程中,我一直面临聚合的特殊问题。 在此代码中, PGDAggregate 类有一个方法 add_to_query ,用于实例化聚合的SQL实现并将其设置为类变量( aggregate )我将用于从另一个文件中调用默认SQL聚合(django / db.models.sql.aggregates.Aggregate)中的 as_sql 方法。
我的代码(第一个文件,我如何实现聚合):
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)'
这就是我尝试使用第二个文件中的aggregate属性(Default SQL Aggregate的一个实例)来调用 as_sql 方法。
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]
我在这个实现中得到的错误(在l:6中)是
异常'BinSort'对象没有属性'aggregate'
作为调试步骤,我尝试使用
检查BinSort实例是否具有属性“aggregate”hasattr(sortx, 'aggregate')
这让我失败了。但是当我尝试通过从add_to_query方法中打印聚合属性来检查时,我可以非常看到该属性被打印出来。 另外,我已经按照Django 1.8 doc https://github.com/django/django/blob/stable/1.8.x/django/db/models/aggregates.py#L46
中指定的方式实现了这一点答案 0 :(得分:0)
虽然这不是解释意外行为的解决方案,但这有效。因为我想使用Default SQL Aggregate类的as_sql()方法,所以我直接初始化了BinSortSQL而不是BinSort类,并使用了它的as_sql()方法。
sortx = BinSortSQL(col, offset=x, bincount=xbin, max=x1)
sorty = BinSortSQL(col, offset=y, bincount=ybin, max=y1)