I have an event that occurs everyday. Here is the model
class Event(models.Model):
date = models.DateField()
plays = models.IntegerField()
viewers = models.IntegerField()
time = models.FloatField()
I am trying to aggregate the event by consecutive days (Friday, Saturday, Sunday).
Note: Friday and Saturday are from the same week and Sunday from the next. Using raw queries (postgres) I can use the following query to aggregate
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")
However in django I can't find a way to replicate the query
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')
However, this query picks Sunday, Friday and Saturday in the same week.