I have this in my header:
from django.db.models import Sum
And this is my function:
def matriz_cxp(request, idProveedor=None):
total = 0
if idProveedor:
form = Filtro_Proveedor_Form({'proveedor': idProveedor})
lista_cxp = CuentaPagar.objects.filter(proveedor=idProveedor)
else:
form = Filtro_Proveedor_Form()
lista_cxp = CuentaPagar.objects.all()
total = lista_cxp.aggregate(Sum('subtotal'))
print(total) # provisional
if request.method == 'POST':
form = Filtro_Proveedor_Form(request.POST)
if form.is_valid():
idProveedor = request.POST.get("proveedor")
return redirect("matriz_cxp", idProveedor)
return render(request, 'matriz_cxp__salud.html', {
'lista_cxp': lista_cxp,
'total': total,
'form': form,
})
But that print
prints this:
{'subtotal__sum': 16000}
And I need this:
16000
How do I modify the function call?
The function
aggregate
simply adds the field to you as a dictionary key. You can try doingprint
it like this:This happens because you are not passing the parameter to the function and it is taking the name of the field but it is possible to pass it a specific name: