Django - Annotate sum of duration in hour

Viewed 64

I have a field Time with type DurationField and I want to sum the values and convert it into decimal hour

Task.objects.all().annotate(total=Sum('Time'))

For example if total = timedelta(seconds=5400) I want to get 1.5 hours

1 Answers

You can divide the duration by timedelta(hours=1), so you render this with:

mytask.total / timedelta(hours=1)

This then produces:

>>> timedelta(seconds=5400) / timedelta(hours=1)
1.5
Related