I have created a function to get some date from database, make some calculations and show it in HTML files. However I could not to get just values from views.
def list_pay(request):
item = Pay.objects.all()
# Quantidades
q_total = Pay.objects.all().count
q_open = Pay.objects.filter(status=0).count
q_paied = Pay.objects.filter(status=1).count
# Soma dos valores
v_total = Pay.objects.aggregate(Sum('value'))
v_open = Pay.objects.filter(status=0).aggregate(Sum('value'))
v_paied = Pay.objects.filter(status=1).aggregate(Sum('value'))
data = {
'item':item,
'v_total':v_total,
'v_open':v_open,
'v_paied':v_paied,
'q_total':q_total,
'q_open':q_open,
'q_paied':q_paied
}
return render(request, 'pay/list_pay.html', data)
<div class="row d-flex justify-content-center">
<h3 class="mb-3 mt-3">Lista <span class="text-success">de pagamentos</span></h3>
<div class="col m-3 p-3">
<label>Total de boletos</label>
<h1 class="text-primary"> {{ q_total }}</h1>
<h1 class="text-primary"> {{ v_total }}</h1>
</div>
<div class="col m-3 p-3">
<span>Quantidade de boletos</span>
<h1 class="text-success">{{ q_paied }}</h1>
<h1 class="text-success">{{ v_paied }}</h1>
</div>
<div class="col m-3 p-3">
<span>Quantidade de boletos</span>
<h1 class="text-danger">{{ q_open }}</h1>
<h1 class="text-danger">{{ v_open }}</h1>
</div>
</div>
The current value loaded = {‘value__sum’: 1831}. The expected output = 1831
I alredy try .values() and transform the dict in a list and other format.
2
Answers
Perform a lookup, so:
or, you can perform the lookup in the view:
That being said, you are currently performing multiple queries that can be combined in one SQL query.
Indeed, we can use:
So here we make at most two database queries.
You should be able to get all of this in a single aggregate query with something like