Calculations

64 views
Skip to first unread message

Abdou KARAMBIZI

unread,
Jul 6, 2023, 12:37:56 PM7/6/23
to django...@googlegroups.com
class LoanInformation(models.Model):
    id = models.AutoField(primary_key=True)
    customer = models.ForeignKey(Customer,on_delete=models.CASCADE)
    amount_to_pay = models.IntegerField()
 
    def __str__ (self):
        return self.id

class Payments(models.Model):
    id = models.AutoField(primary_key = True)
    loan = models.ForeignKey(LoanInformation,on_delete=models.CASCADE)
    amount = models.IntegerField()
    paidon = models.DateField(auto_now=True)

    def __str__ (self):
        return self.id



I want to display amount_to_pay - sum of amount 

Abdou KARAMBIZI

unread,
Jul 6, 2023, 12:39:50 PM7/6/23
to django...@googlegroups.com
I want to display the result of  "amount_to_pay - sum of amount"

Thomas Couch

unread,
Jul 7, 2023, 11:03:09 AM7/7/23
to Django users
Hi Abdou, have a look at aggregation (https://docs.djangoproject.com/en/4.2/topics/db/aggregation/)

In this case I think you'll want something like:

```
from django.db.models import Sum

# Where loan = the LoanInformation instance you're interested in
outstanding_balance = loan.amount_to_pay - loan.payments_set.aggregate(Sum("amount"))
```

Thomas Couch

unread,
Jul 7, 2023, 11:31:25 AM7/7/23
to Django users
Or, you can combine annotations and expressions to do something like this:

qs = LoanInformation.objects.annotate(outstanding=F("amount_to_pay") - Func(F("payments__amount"), function="SUM))
loan = qs.get(id=loan_id)
loan.outstanding

Clement Idemudo

unread,
Jul 8, 2023, 10:14:45 AM7/8/23
to Django users
in addition to what @Thomas said you need to first perform an aggregated sum on the payments model hence the need for ... loan.payments_set.aggregrate(models.Sum('amount'))
the above returns the sum of all payment related to your loan instance from which you can now subtract from loan.amount_to_pay

you can read this post for further clarity https://medium.com/@clemcy9/performing-aggregate-sum-in-postgresql-django-3472e5248bb3

Abdou KARAMBIZI

unread,
Jul 10, 2023, 6:03:06 AM7/10/23
to django...@googlegroups.com
Hello Thomas 

I have tried but I got that error message

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1b059aab-1b8e-4a8e-b9e3-d7cb9d1f5673n%40googlegroups.com.
django_error.JPG

Thomas Couch

unread,
Jul 10, 2023, 6:16:53 AM7/10/23
to Django users
Ah ok, looks like aggregate always returns a dictionary. This should work:

outstanding_balance = loan.amount_to_pay - loan.payments_set.aggregate(paid=Sum("amount"))['paid']

Abdou KARAMBIZI

unread,
Jul 10, 2023, 7:19:13 AM7/10/23
to django...@googlegroups.com
Thanks Thomas, now it is working properly

Reply all
Reply to author
Forward
0 new messages