0

I'm trying to make a personal WebApp with Django/Python, all works out so far, just that i want to display: total ammount, balance ect...... in my WebApp and i can't figure it out how to do is. Can someone help me out here? Thanks! The error i am recieving on line: total = Bills.objects.aggregate(Sum('ammount')) - "Bills is not defined"

Models.py

class Bills(models.Model):
    bank = models.CharField(max_length=40)
    name = models.CharField(max_length=40)
    ammount = models.DecimalField(max_digits=10, decimal_places=2)
    total = Bills.objects.aggregate(Sum('ammount'))

    def __str__(self):
        return self.bank

Views.py

def index(request):
    return render(request, 'budget/index.html')

def bills(request):
    bills_list = Bills.objects.order_by('id')
    context = {'bills_list': bills_list}
    return render(request, 'budget/bills.html', context)
  • re-reading your question, you dont have a Balance field, where would you get that? – Walucas Dec 03 '18 at 11:11
  • That's also a part of the question, i have know idea how to do this, i can make a field in my models for a balance (variable), but i don't know how to get the sum of the 'amount' for instance..... – arcodedale Dec 03 '18 at 11:44
  • First google result: https://stackoverflow.com/questions/8616343/django-calculate-the-sum-of-the-column-values-through-query – Walucas Dec 03 '18 at 11:45
  • i tried making a variable: total = Bills.objects.aggregate(Sum('ammount')) but than i get the message that 'Bills' in not defined..... this i did before posting this question. – arcodedale Dec 03 '18 at 11:51

2 Answers2

0

Import your Bills on your view, then:

total = Bills.objects.all().annotate(Sum('amount'))

Then you can use it on your template. Its not a property of your Bills class.

Walucas
  • 2,549
  • 1
  • 21
  • 44
0

try this

class Bills(models.Model):
    bank = models.CharField(max_length=40)
    name = models.CharField(max_length=40)
    ammount = models.DecimalField(max_digits=10, decimal_places=2)

    @property
    def total(self):
        return self.__class__.objects.all().aggregate(sum=Sum('amount')).get('sum')

    def __str__(self):
        return self.bank

I don't know why you need total on the object. But this will get total on each object. And you can simply call them by bill.total if bill is an object.

NOTE: total is not the property of object. So add a class method for finding the total. That is the best practice.

class Bills(models.Model):
    bank = models.CharField(max_length=40)
    name = models.CharField(max_length=40)
    ammount = models.DecimalField(max_digits=10, decimal_places=2)

    @classmethod
    def total(self):
        return self.__class__.objects.all().aggregate(sum=Sum('amount')).get('sum')

    def __str__(self):
        return self.bank

Now you can find total by calling 'Bills.total()'. And also use singular names for models like Bill rather than Bills.

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
  • Sorry to ask you again but i added the classmethod in my 'Bills" model, but by mistake i added it also in my views.... it worked but i feel i am doing something wrong.... can you please tell me where i should make the call and how should that code being written? thanks a lot! – arcodedale Dec 06 '18 at 15:47
  • If you are using class method approach, you should update the context to `context = {'bills_list': bills_list, 'total': Bills.total()}`. Now you can use `total` in your template('budget/bills.html'). Hope this will help. – Aneesh R S Dec 07 '18 at 06:12