Django cumulative sum in model (sqlite)

Since django-2.0, it is possible to work with a Window expressions [Django-doc]:

from django.db.models import F, Sum, Window

transaction.objects.annotate(
    cumsum=Window(Sum('amount'), order_by=F('transactiondate').asc())
).order_by('transactiondate', 'cumsum')

The transaction objects that arise from this will have an extra attribute .cumsum.


Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from transaction to Transaction.