【问题标题】:django get sum of column based on where clausedjango 根据 where 子句获取列的总和
【发布时间】:2014-04-09 16:32:41
【问题描述】:

我已阅读与 django 聚合相关的文档,但我仍然无法弄清楚如何准确地获得每个用户的总积分。

这是我尝试过的:

ModelName.objects.filter(username=rUser.username).aggregate(Sum('points'))

我收到以下错误:

Request Method: GET
Request URL:    http://localhost:8001/mypage/
Django Version: 1.4.8
Exception Type: NameError
Exception Value:    
global name 'Sum' is not defined

【问题讨论】:

    标签: python django django-queryset


    【解决方案1】:

    你需要先导入Sum

    from django.db.models import Sum
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2017-05-12
      • 2019-08-18
      • 2020-04-28
      • 2020-07-18
      相关资源
      最近更新 更多