尝试对过滤后的对象求和 Django

Trying to SUM filtered objects django

伙计们,我真的是 Django 的新手,我正在尝试像这样按日期时间对过滤后的对象列表求和 SQL

SELECT ((select sum(some_col) from some_table WHERE datte> now()-interval 1 day))

我试过这样的事情:

inidate = datetime.datetime.now() 
d_date= timezone.now() - timedelta(1)
query = Model.objects.filter(date__range=(d_date,inidate)).aggregate(sum('values'))

结果>>>

TypeError: unsupported operand type(s) for +: 'int' and 'str'

你应该使用 Sum expression [Django-doc], not the sum(…) builtin [python-doc],所以:

from django.db.models import <strong>Sum</strong>

query = Model.objects.filter(
    date__gte=timezone.now() - timedelta(1)
).aggregate(<strong>Sum(</strong>'values'<strong>)</strong>)