views:

84

answers:

1

I'd like to filter objects to the day with datetime, but can't find examples on how to do this anywhere.

This, for example, works perfectly in pulling together all following events:

@login_required
def invoice_picker(request):
    """Grab a date from the URL and show all the invoicable deliveries for that day."""
    query = request.GET.get('q' , '2/9/1984')
    date = datetime
    date = datetime.strptime('7/14/2010', '%m/%d/%Y')
    date = date.strptime(query, '%m/%d/%Y')
    results = []
    if query:
        results = LaundryDelivery.objects.filter(end__gte=date)
    return render_to_response('invoicer.html', {
        'results' : results,
        'date' : date,
        'user' : request.user,
    })

But when I remove __gte, it returns nothing because the dates are along the lines of 2010-06-22 04:04:00 instead of 2010-06-22 00:00:00. I also tried:

 results = LaundryDelivery.objects.filter(end.date=date)

but unfortunately I get the error "keyword can't be an expression". Any ideas?

+3  A: 

I don't think there's a good way to compare datetimes with dates. One way is the following:

filter(end__year=date.year, end__month=date.month, end__day=date.day)

The other is to use the range lookup with the min time and max time for the day.

ars
Thanks Ars. Works perfectly.
Michael Morisy
Sure, glad to help. :)
ars