我正在尝试总结用户每天花费的时间。我想将其转换为整数或小数来表示小时(例如 2、6.5),以便轻松地将其绘制到我的图表中。但下面代码的结果采用这种格式HH:mm:ss。任何人都可以帮我解决这个问题吗?
day = Clocking.objects.filter(clockout__isnull=False, user=nid).annotate(date=TruncDate('clockin'))
.values('date').annotate(total=Sum(F('clockout') - F('clockin'))).values('total')
这是我的 models.py
class Clocking(models.Model):
user= models.ForeignKey('User', models.DO_NOTHING)
clockin = models.DateTimeField()
clockout = models.DateTimeField(null=True)
基于Func使用 sql 函数
TIMEDIFF
thenTIME_TO_SEC
,将返回秒: