0

My code, models.py

class accounts(models.Model):
    paid = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    balance = models.DecimalField(max_digits=8, decimal_places=2, default=0)

Views.py

def home(request):
    template = "index.html"
    total_paid = Accounts.aggregate(Sum('paid'))
    total_balance = Accounts.aggregate(Sum('balance'))
    return render_to_response(template, context_instance=RequestContext(request,locals()))

And index.html

 {{ total_balance }}
 {{ total_paid }}

I want to render the amount of this fields on all users , so i use "Sum" , and i want to render it , but when i do it doesnt render the raw value , for some reason i cant undestand they return {'balance__sum': Decimal('0.00')} , i suppose its "Sum's" fault i guess

1 Answers1

0

The aggregate returns a dictionary as defined here https://docs.djangoproject.com/en/dev/topics/db/aggregation/. So if you want to render is you can do the following

def home(request):
    template = "index.html"
    total_paid = Accounts.objects.all()aggregate(Sum('paid'))
    total_balance = Accounts.objects.all()aggregate(Sum('balance'))

    context ('total_paid': total_paid['paid_sum'],
             'total_balance': total_balance['balance_sum'])
    return  render(request, template, context)

the key is that you need to somehow extract the keys returned by the aggregate funcion to the context and then use it in your template

jax
  • 840
  • 2
  • 17
  • 35