【问题标题】:Instantiating the SQL implementation of Aggregates in Django 1.8在 Django 1.8 中实例化聚合的 SQL 实现
【发布时间】:2015-08-14 12:44:21
【问题描述】:

我一直致力于将使用 Django 1.6 的现有代码库更新到 Django 1.8。在此过程中,我一直面临聚合的特殊问题。 在此代码中,PGDAggregate 类有一个方法 add_to_query,该方法旨在实例化聚合的 SQL 实现并将其设置为类变量(aggregate) 我将用来从另一个文件调用 Default SQL Aggregate(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)'

这就是我尝试使用第二个文件中的聚合属性(默认 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' 对象没有属性 'aggregate'

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

检查 BinSort 实例是否具有“聚合”属性
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

【问题讨论】:

    标签: django django-1.8 django-aggregation


    【解决方案1】:

    虽然这不是解释意外行为的解决方案,但这是可行的。因为我想使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-07-21
      • 2018-06-29
      • 2013-01-08
      相关资源
      最近更新 更多