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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/55AFEF6C.7090600%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to