我的 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"
希望你能帮助我解决这个问题:)