I have a model with a time field, and I need to sum the time fields in all instances.
This is what I've tried:
total_hours = Day.objects.filter(date__year=current_year, date__month=current_month).aggregate(Sum('total_work_hours')) print(total_hours)
and I get this error:
You cannot use Sum, Avg, StdDev, and Variance aggregations on date/time fields in sqlite3 since date/time is saved as text.
Is there a way around this? I'd like to stick with SQLite for now.
Without changing databases, you will have to do this in Python. You can do it rather cheaply by using
values_list to just pull the
total_work_hours for each day, and sum them.
hours = Day.objects.filter( date__year=current_year, date__month=current_month ).values_list('total_work_hours', flat=True) total_hours = sum(hours) print(total_hours)