0

I want to loop through the object fields to update their values but im not quite sure how to do this.

class FinancePending(models.Model):
invoiceNumber = models.IntegerField
amountPaid = models.CharField(max_length=20)
AmountPending = models.IntegerField( blank=True, null=True)
TotalAmount = models.CharField(max_length=50, default=0)

Now i want to calculate amountpending in a function. But its not working

     amount_paid = FinancePending.objects.values_list('amountPaid', flat=True)
        amount_paid = list(amount_paid)
        total_amount = FinancePending.objects.values_list('TotalAmount', flat=True)
        total_amount = list(total_amount)

        # total - paid
        TotalFee = [float(s.replace(',', '')) for s in total_amount]
        AmountPaid = [float(s.replace(',', '')) for s in amount_paid]
        finance_pending = FinancePending.objects.all()
        i = 0
        while i < len(TotalFee):
            amount_pending = TotalFee[i] - AmountPaid[i]
            FinancePending.objects.filter(invoiceNumber=i).values(AmountPending=amount_pending)
            setattr(finance_pending, 'AmountPending', str(amount_pending))
            i = 1 + i

1 Answers1

1

Would suggest using Query Expression, more powerful and robust

prashant
  • 2,808
  • 5
  • 26
  • 41