Hi,
On 07/22/2015 12:54 PM, Nkansah Rexford wrote:
> I currently have:
>
> |
> @classmethod
> defget_past_week(self):
> start_date =datetime.now().date()
> end_date =datetime.now().date()-timedelta(weeks=1)
>
> returnMyModel.objects.filter(pub_date__range=(end_date,start_date)).aggregate(Sum('off_hours'))
> |
>
> which simply pulls all posts from the current date minus 7 days
>
> I want to pull posts from within the same day factoring in the time at
> the moment. Thus if the time is 15:00 GMT now, I want all posts from
> 14:59:49 GMT back to 00:00:01 GMT of the same day.
>
> How can I do something like that?
I'll assume `pub_date` is a `DateTimeField`. If it's a `DateField`, then
this query isn't possible unless you change it to a `DateTimeField`.
You want something like:
from datetime import timedelta
from django.utils import timezone
now = timezone.now()
one_day_ago = now - timedelta(days=1)
return MyModel.objects.filter(pub_date__range=(one_day_ago, now))
Carl