【问题标题】:Using extra kwargs for Aggregate in Django在 Django 中使用额外的 kwargs 进行聚合
【发布时间】:2013-10-20 16:21:53
【问题描述】:

如果您查看 Django 中聚合函数的定义,您会发现它们实际上是 django.db.models.aggregates.Aggregate 的子类,其构造函数如下所示:

class Aggregate(object):
"""
Default Aggregate definition.
"""
def __init__(self, lookup, **extra):
    """Instantiate a new aggregate.

     * lookup is the field on which the aggregate operates.
     * extra is a dictionary of additional data to provide for the
       aggregate definition

    Also utilizes the class variables:
     * name, the identifier for this aggregate function.
    """
    self.lookup = lookup
    self.extra = extra

    #... the rest is truncated

这个额外的关键字参数是做什么用的?我可以使用它们对聚合进行更复杂的查询吗?我试图在上面找到任何文档,但没有成功。我相信它没有记录在案,但无论如何,这些额外的论点是什么,可以用它们做什么?

谢谢。

【问题讨论】:

    标签: python django orm aggregate-functions django-orm


    【解决方案1】:

    当然,

    只需查看来源,extra is unpacked and passed to the "backend-specific" aggregate class

    SQL 后端的后端特定聚合可在 db.models.sql.aggregates 中找到。

    在那里,您会看到Sum 接受distinct 参数,而StdDevVariance 接受sample 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2020-11-09
      • 2015-10-02
      • 1970-01-01
      • 2016-01-19
      • 2013-02-25
      • 2012-01-15
      相关资源
      最近更新 更多