Monday, July 10, 2023

Re: Calculations

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']

On Monday, July 10, 2023 at 11:03:06 AM UTC+1 Abdou KARAMBIZI wrote:
Hello Thomas 

I have tried but I got that error message

On Fri, Jul 7, 2023 at 5:03 PM Thomas Couch <t.c...@ucl.ac.uk> wrote:
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"))
```


On Thursday, July 6, 2023 at 5:39:50 PM UTC+1 Abdou KARAMBIZI wrote:
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 the result of  "amount_to_pay - sum of amount"

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9be67e3f-785b-4c39-ab6c-309fd33a8289n%40googlegroups.com.

No comments:

Post a Comment