[英]calculation sum of a django model field
class Transaction:
amount = models.FloatField()
Now I calculate the sum of amount 现在我计算金额的总和
transactions = Transaction.objects.all()
amount = 0
for transaction in transactions:
balance = transaction.amount
amount += balance
I know it is calculate by using agreegate. 我知道它是通过使用同意门来计算的。 But is this possible by this way or another way ?
但这可能通过这种方式还是另一种方式呢?
ModelName.objects.aggregate(Sum('field_name'))
这是文档
Not sure why not use aggregate, if you wanna iterate you can try 不知道为什么不使用聚合,如果您想迭代,可以尝试
transactions = Transaction.objects.values_list('amount', flat=True)
amount = sum(list(transactions))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.