首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Django 1.8中实例化Aggregates的SQL实现

在Django 1.8中实例化Aggregates的SQL实现
EN

Stack Overflow用户
提问于 2015-08-14 12:44:21
回答 1查看 184关注 0票数 1

我一直在更新使用Django 1.6到Django 1.8的现有代码库。在此过程中,我一直面临一个与聚合有关的特殊问题。在这段代码中,PGDAggregate类有一个方法add_to_query,用于实例化聚合的SQL实现,并将其设置为类变量(聚合),用于从另一个文件调用默认的as_sql方法。

我的代码(第一个文件,如何实现聚合):

代码语言:javascript
复制
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方法。

代码语言:javascript
复制
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实例是否具有属性“聚合”,使用

代码语言:javascript
复制
hasattr(sortx, 'aggregate')

把我搞错了。但是,当我试图通过从add_to_query方法内部打印聚合属性来进行检查时,我很可能会看到这个属性正在被打印。另外,我已经按照Django 1.8doc,https://github.com/django/django/blob/stable/1.8.x/django/db/models/aggregates.py#L46中指定的方式实现了这一功能。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-18 08:25:54

虽然这不是一个解释意外行为的解决方案,但这是可行的。由于我想使用默认SQL类的as_sql()方法,所以我直接初始化了BinSortSQL,而不是BinSort类,并且使用了as_sql()方法。

代码语言:javascript
复制
    sortx = BinSortSQL(col, offset=x, bincount=xbin, max=x1)
    sorty = BinSortSQL(col, offset=y, bincount=ybin, max=y1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32010449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档