[Django]-Django Running Balance / Cumulative Balance

2👍

First remove your @property balance function

then in views.py

def index(request):
    all_transactions = Transaction.objects.annotate(cumsum=Func(Sum('amount'),template='%(expressions)s OVER (ORDER BY %(order_by)s)',order_by="id"))
    context = {
        "transactions": all_transactions
    }
    return render(request,'pages/index.html', context)

Then in your index.html

{% for each_transaction in transactions %}
<tr>
    <td>{{each_transaction.transaction_date}}</td>
    <td>{{each_transaction.account_nickname}}</td>
    <td>{{each_transaction.amount}}</td>
    <td>{{each_transaction.cumsum}}</td>
</tr>
{% endfor %}

Leave a comment