I am using the following query to get a sum per day:
orders = OrderM.objects.filter(user=request.user) \
.annotate(day=TruncDay('datetime_added')) \
.values('day') \
.annotate(sum=Sum(F('orderd__good_count'))) \
.order_by('-day')
as expected each day changes at 00:00 midnight! I want to change this behaviour so that the sum to be generated on periods from 06:00 to 06:00 of each day. I think that the use of datetime__range = ..
will do, but I'm not experienced in django queries.
Thanks...
This seems to do the work:
Time portion of
TruncDay('datetime_added')
is 00:00 midnight by definition. So we have to subtract 6 hours from eachdatetime
field before we truncate. The following will actually change the day portion, after 6 in the morning!The use of
ExpressionWrapper
is essential, since I get, otherwise (and I don't know why!).