Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django get sum of column based on where clause

I have read the documentation related to django aggregation but I am still have trouble figuring out how exactly I can get a sum of points per user.

Here is what I tried:

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

I get the following error:

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
like image 463
add-semi-colons Avatar asked Dec 25 '22 10:12

add-semi-colons


1 Answers

You need to import Sum first:

from django.db.models import Sum
like image 117
alecxe Avatar answered Jan 04 '23 22:01

alecxe