Home > Back-end >  django postgresql cumulative sum
django postgresql cumulative sum

Time:09-16

I am trying to make a party ledger. which details will show by a date search result. I have a column name balance which will display cumulative data by the date search. The balance field is a decimal field. I am trying the following in views.py

          def partyDetails(request,pk):
               form = DateRangeForm()
               search = []
               until = []
               cumbalance = 0.00
               if request.method == 'POST':
                   form = DateRangeForm(request.POST or None)
                     if form.is_valid():
                     search = PurchasePayment.objects.filter(vendor=pk,date__range=(
                       form.cleaned_data['start_date'],
                       form.cleaned_data['end_date']
                      ))
                     for balancet in search:
                        cumbalance  = Decimal(balancet.balance)

           else:
              return redirect('party_ledger')
           return render(request, 'purchase/party_details.html', {'dateform':form, 
                         'party':search,'name':pk, 'cumbalance':cumbalance})

But I am getting error unsupported operand type(s) for = If I try cumbalance = [balancet.balance] then get [Decimal('200000.00'), Decimal('-200000.00')] MY Models.py are given bellow

  class PurchasePayment(models.Model):
        id = models.AutoField(primary_key=True)
        date = models.DateField(default=date.today)
        invoice = models.CharField(max_length=20)
        vendor = models.CharField(max_length=50)
        amount = models.DecimalField(max_digits=9, decimal_places=2, default=0.00)
        discount = models.DecimalField(max_digits=9, decimal_places=2, default=0.00)
        payment = models.DecimalField(max_digits=9, decimal_places=2, default=0.00)
        balance = models.DecimalField(max_digits=9, decimal_places=2)
        remarks = models.TextField(blank=True)
        extra = models.CharField(max_length=100, blank=True)

         def __str__(self):
           return self.vendor

My HTML

                        <tbody>
                              {% for data in party %}
                              {% for cum in cumbalance %}
                           <tr>
                            <td >{{data.date}}</td>
                            <td >{{data.invoice}}</td>
                            <td >{{data.amount}}</td>
                            <td >{{data.discount}}</td>
                            <td >{{data.payment}}</td>
                            <td >{{cum}}</td>                               
                        </tr>
                         {% endfor %}
                         {% endfor %}
                         </tbody>

How can I make a cumulative view in balance field

CodePudding user response:

You can just do

from django.db.models import Sum

cumbalance = search.aggregate(cumbalance=Sum('balance'))['cumbalance']

CodePudding user response:

Have you tried using aggregate?

cumulative_balance = PurchasePayment.objects.filter(vendor=pk,date__range=(form.cleaned_data['start_date'], form.cleaned_data['end_date']).aggregate(Sum('balance'))

Then to access the value you use cumulative_balance['balance']

  • Related