1

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

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
MichaelS
  • 259
  • 2
  • 13

1 Answers1

0

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
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555