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)
4
  • re-reading your question, you dont have a Balance field, where would you get that? Commented Dec 3, 2018 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..... Commented Dec 3, 2018 at 11:44
  • First google result: stackoverflow.com/questions/8616343/… Commented Dec 3, 2018 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. Commented Dec 3, 2018 at 11:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.