我尝试计算2个DateTimeField之间的差异,以了解每周的总工作量(仅小时),而我只希望几天使用相同的功能。
在另一个Stackoverflow问题之后,我发现这样的聚合:
total_hours_per_week = (Timesheet.objects.filter(
owner = request.user.pk,
week = datetime.datetime.now().isocalendar()[1]
)
.annotate(
total_hours_per_week=Func(F('end'), F('start'), operator='+', function='age')
))
当前,我已经好几天没有该功能了(如果您也可以帮助她),但是以前的汇总返回了此错误: “没有这样的功能:年龄”
您可以在下面阅读我的模型:
class Timesheet(models.Model):
owner = models.ForeignKey(User, on_delete = models.CASCADE)
title = models.CharField(max_length = 64, verbose_name = _("Title"))
start = models.DateTimeField(verbose_name = _("Start time"))
end = models.DateTimeField(verbose_name = _("End time"))
allDay = models.BooleanField(blank = True, default = False, verbose_name = _("All day"), help_text = _("If it's a full day work, just click 'Now' for start/end"))
week = models.IntegerField(verbose_name = "working week")
def __str__(self):
return "{}".format(self.title)
def save(self, *args, **kwargs):
if not self.pk:
self.week = datetime.datetime.now().isocalendar()[1]
if self.allDay:
self.start = datetime.datetime(year = datetime.datetime.today().year, month = datetime.datetime.today().month, day = datetime.datetime.today().day, hour=8, minute=00)
self.end = datetime.datetime(year = datetime.datetime.today().year, month = datetime.datetime.today().month, day = datetime.datetime.today().day, hour=17, minute=30)
super(Timesheet, self).save(*args, **kwargs)
希望您能帮助我解决我的错误。
谢谢你!