我有一个模型:
class ItemPrice(models.Model):
price = models.DecimalField(max_digits=8, decimal_places=2)
# ...
我尝试使用以下代码计算这个查询集中 price
的总和:
items = ItemPrice.objects.all().annotate(Sum('price'))
这个查询有什么问题吗?还有其他什么方法可以计算price
列的总和吗?
我知道可以在查询集上使用for循环来完成,但我需要一种优雅的解决方案。
谢谢!