[Fixed]-Sum content from looping python

1๐Ÿ‘

โœ…

I think you could try one of this:

  • Sum inside the for loop

    total = 0
    for obj in dataangsuran:
        total = total + obj.cangsuranpokok
        col = [
          str(obj.ckarid),
          str(obj.ckarid.cnik_nip),
          total,
          str(obj.cangsuranpokok),
        ]
    

And then use total

  • Take the sum from ORM

    from django.db.models import Sum
    dataangsuran.aggregate(total=Sum('cangsuranpokok'))
    

Keep in mind that dataangsuran is a QuerySet object, so you can add the aggregate after the first loop, when you write the excel file.

๐Ÿ‘คGocht

Leave a comment