【发布时间】:2019-04-29 00:43:43
【问题描述】:
我的 django 模型中有一个 TimeField()。我想将这个记录的总和转换为小时。
这是我认为的实际代码:
hours_week_decimal = Timesheet.objects.filter(owner = request.user.pk, week = datetime.datetime.now().isocalendar()[1]).aggregate(total=Sum('working_hour')) # this method return a dict of decimal
total_hours_week = convertDecimalToHours(hours_week_decimal)
及相关功能:
def convertDecimalToHours(times):
total_time = 0
for k, v in times.items():
total_time += int(v)
print("type: {} - value: {}".format(type(total_time), total_time))
这让我回来了:
type: int - value: 166000
我有两个小时:
Monday (08:30) and Tuesday(08:30)
它一定是回了我"17:00"
希望你能帮我解决这个问题:)
【问题讨论】:
-
如果你分享
dict的例子会容易得多。 -
你用谷歌搜索过吗?和这个stackoverflow.com/a/6706329/10035556有关系吗?
-
我不知道为什么你期望像
17:00这样的输出,而你的函数输出是print("type: {} - value: {}".format(type(total_time), total_time)),所以函数convertDecimalToHours(times)不会产生你想要的。 -
@Ssein 看起来字典的值类似于 083000 和 083000,其总和为 166000,但他预计输入时间为 08:30 和 08:30,因此输出应为 170000不是 166000
-
是的,你说的有道理,但是从他刚才问的内容来看,并没有那么清楚。
标签: python django datetime django-aggregation timefield