Django SUM Query?

前端 未结 1 1520
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 06:59

I have a query akin to the following:

SELECT SUM(ISNULL(table.name)) FROM table

How does that SUM

相关标签:
1条回答
  • 2021-01-31 07:26

    Update: The following incorporates the ISNULL aspect of the original query:

    from django.db.models import Sum
    
    ModelName.objects.filter(field_name__isnull=True).aggregate(Sum('field_name'))
    # returns {'field_name__sum': 1000} for example
    

    You're looking for the Sum aggregation function, which works as follows:

    ModelName.objects.aggregate(Sum('field_name'))
    

    See: https://docs.djangoproject.com/en/dev/ref/models/querysets/#sum

    0 讨论(0)
提交回复
热议问题