简体   繁体   中英

Django - Annotate sum of duration in hour

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM