I want to pass this Mysql code to Django how can I do it? [closed]

1

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.

    
asked by Angie Lizeth Santiago Piñeros 09.09.2016 в 19:10
source

1 answer

2

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 .

    
answered by 09.09.2016 в 21:29