Reputation: 1581
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 ?
Upvotes: 0
Views: 820
Reputation: 754
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))
Upvotes: 0