我一直致力于将使用 Django 1.6 的现有代码库更新到 Django 1.8。在此过程中,我一直面临聚合的特殊问题。在这段代码中,PGDAggregate类有一个方法add_to_query旨在实例化聚合的 SQL 实现并将其设置为类变量(聚合),我将使用它来调用默认 SQL 聚合中的as_sql方法(django/db .models.sql.aggregates.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)'
这就是我尝试使用第二个文件中的聚合属性(默认 SQL 聚合的实例)来调用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”对象没有属性“聚合”
作为调试步骤,我尝试检查 BinSort 实例是否具有属性“聚合”,使用
hasattr(sortx, 'aggregate')
这使我返回了错误。但是,当我尝试通过从 add_to_query 方法内部打印聚合属性来检查时,我可以看到打印的属性。此外,我已经按照 Django 1.8 文档中指定的方式实现了这一点,https://github.com/django/django/blob/stable/1.8.x/django/db/models/aggregates.py#L46