Hello, someone who helps me, I want to pass this code to Django and Python as I can.
SELECT 'clasificador', SUM('total') AS t_total FROM clasificacion
GROUP BY 'clasificador'
I would really appreciate your help.
Hello, someone who helps me, I want to pass this code to Django and Python as I can.
SELECT 'clasificador', SUM('total') AS t_total FROM clasificacion
GROUP BY 'clasificador'
I would really appreciate your help.
To convert the SQL query you want, you must use the Django aggregation functions. And since you are going to group them, you must use the annotate
function.
This is a way to do it.
from django.db.models import Sum
query = Clasificacion.objects\
.annotate(t_total=Sum(total))\
.values('clasificador', 't_total')
When using annotate
to apply the function Sum
, the place where the clause values()
is found impacts the way in which the query is generated.
You can consult the documentation here: link .