我有一个每天都会发生的事件。这是模型
class Event(models.Model):
date = models.DateField()
plays = models.IntegerField()
viewers = models.IntegerField()
time = models.FloatField()
我正在尝试按连续天(周五、周六、周日)汇总事件。
注意:周五和周六是同一周,周日是下一周。使用原始查询(postgres)我可以使用以下查询来聚合
SELECT DATE_TRUNC('week', "date")::date AS date,
SUM(plays) AS total_plays,
SUM(viewers) AS total_views,
SUM(time) AS total_time
FROM app_event
WHERE extract(isodow FROM date) IN (5,6,7)
GROUP BY date_trunc('week',"date")
但是在 django 我找不到复制查询的方法
queryset = Event.objects.filter(date__week_day__in=[1, 6, 7])\
.annotate('week'=TruncWeek('date')) \
.values('week')\
.annotate(total_views=Sum('views'), total_plays=Sum('plays'), total_time=Sum('time')) \
.values('week', 'total_views', 'total_time', 'total_plays')
但是,此查询选择同一周内的星期日、星期五和星期六。