9👍
✅
You can use the extra
method to add in the year and date values before doing the aggregation.
Somewhere.objects.extra(select={'year': 'EXTRACT(year FROM date)',
'month': 'EXTRACT(month FROM date)'}
).values_list('year', 'month').annotate(Sum('amount'))
1👍
Take a look at Complex lookups with Q objects and Generating aggregates for each item in a QuerySet.
- [Django]-What are some of the core conceptual differences between C# and Python?
- [Django]-Django & GDAL – Could not find the GDAL library
- [Django]-Django – Save modelForm even if no field are changed on POST
Source:stackexchange.com