Python - Django MySql raw sum returns single value

Beginning in Django, I try to query data from MySQL and running with an issue while I try to use raw.

I return, I correct values, grouped, but not summed (only return last value for that group I guess). My query works good when I run it on MySQL directly, just not with objects.raw.

Any idea why that is / How to work with that? My code is as below.

yesterday = date.today() - timedelta(days=1)
list_parts = PqList.objects.raw('SELECT id, SUM(quantity), equipment_id, so_number FROM pq_list WHERE date > %s group by equipment_id, so_number', [yesterday])
context['list_parts'] = list_parts

ok, figured it out myself, I did add '''as quantity''' behind the sum and now works out correctly, I get the correct value. Updated code below.

yesterday = date.today() - timedelta(days=1)
            list_parts = PqList.objects.raw('SELECT id, SUM(quantity) as quantity, equipment_id, so_number FROM pq_list WHERE date > %s and so_number > -1 group by equipment_id, so_number', [yesterday])
            context['list_parts'] = list_parts
Back to Top