[英]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我有一个类型为 DurationField 的时间字段,我想对这些值求和并将其转换为十进制小时
Task.objects.all().annotate(total=Sum('Time'))
For example if total = timedelta(seconds=5400) I want to get 1.5 hours例如,如果 total = timedelta(seconds=5400) 我想得到 1.5 小时
You can divide the duration by timedelta(hours=1)
, so you render this with:您可以将持续时间除以timedelta(hours=1)
,因此您可以使用以下内容进行渲染:
mytask.total / timedelta(hours=1)
This then produces:这会产生:
>>> timedelta(seconds=5400) / timedelta(hours=1)
1.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.