在 Django 1.8 中实例化聚合的 SQL 实现

Instantiating the SQL implementation of Aggregates in Django 1.8

我一直致力于将使用 Django 1.6 的现有代码库更新到 Django 1.8。在此过程中,我一直面临着聚合的特殊问题。 在此代码中,PGDAggregate class 有一个方法 add_to_query 旨在实例化 SQL 实现聚合并将其设置为 class 变量(聚合),我将使用它来调用 as_sql 方法默认 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)得到的错误是,

exception 'BinSort' object has no attribute 'aggregate'

作为调试步骤,我尝试使用

检查 BinSort 实例是否具有属性 "aggregate"
hasattr(sortx, 'aggregate')

返回了 False。但是,当我尝试通过从 add_to_query 方法内部打印聚合属性来进行检查时,我可以非常清楚地看到正在打印的属性。 此外,我已经按照 Django 1.8 文档中指定的方式实现了这一点,https://github.com/django/django/blob/stable/1.8.x/django/db/models/aggregates.py#L46

虽然这不是解释意外行为的解决方案,但它确实有效。因为我想使用默认 SQL 聚合 class 的 as_sql() 方法,所以我直接初始化了 BinSortSQL 而不是 BinSort class 并且使用它的 as_sql() 方法。

    sortx = BinSortSQL(col, offset=x, bincount=xbin, max=x1)
    sorty = BinSortSQL(col, offset=y, bincount=ybin, max=y1)