aggregate和annotate的区别

简介: aggregate和annotate的区别。

aggregate和annotate的区别
聚合函数不可以单独执行, 需要放在一些可以执行聚合函数的方法下面才能够执行.

  1. aggregate:可以执行聚合函数, 并将执行后的结果以字典的形式返回。
    这个字典中key就是字段_聚合函数的名字, value就是聚合函数执行的结果

    def first(request):
        a = Question.objects.aggregate(Sum('choice__votes'))
        print(a)
        return HttpResponse('a')
    
    # {'votes__sum': 2}
    # 如果不想使用默认的key, 可以采用下面的方法对key进行更改, 如下将key更改为summm
    a = Choice.objects.aggregate(summm=Sum('votes'))
    # {'summm': 2}
    
相关文章
|
10月前
Stream方法使用-filter、sorted、distinct、limit
Stream方法使用-filter、sorted、distinct、limit
61 0
ABAP关键字组合LOOP AT GROUP BY的用法
ABAP关键字组合LOOP AT GROUP BY的用法
883 0
|
关系型数据库 PostgreSQL
PostgreSQL listagg within group (order by) 聚合兼容用法 string_agg ( order by) - 行列变换,CSV构造...
标签 PostgreSQL , order-set agg , listagg , string_agg , order 背景 listagg — Rows to Delimited Strings The listagg function transforms values from a g...
5988 0
|
数据建模
白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析
白话Elasticsearch59-数据建模实战_ Nested Aggregation/ Reverse nested Aggregation对嵌套的博客评论数据进行聚合分析
60 0
|
SQL Oracle 关系型数据库