views:

1637

answers:

6

I am trying to filter a DateTimeField comparing with a date. I mean:

MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22))

I get an empty queryset list as an answer because (I think) I am not considering time, but I want "any time".

Is there an easy way in Django for doing this?

* I have the time in the datetime setted, it is not 00:00.

+3  A: 
YourModel.objects.filter(datetime_published__year='2008', 
                         datetime_published__month='03', 
                         datetime_published__day='27')

// edit after comments

YourModel.objects.filter(datetime_published=datetime(2008, 03, 27))

doest not work because it creates a datetime object with time values set to 0, so the time in database doesn't match.

zalew
This is the correct way. Django documentation ref?
hughdbrown
thx for the answer! the first alternative doesn't work with datetimefields. The second alternative works ;). If someone knows another method please answer
Xidobix
http://docs.python.org/library/datetime.html#datetime-objects using datetime() from datetime module hrs,mins,secs is optional.the second is from a working project with vars replaced, you can look in the docs it's correct
zalew
i know it is optional, the problem is that my datetimefield has the time setted, it is not 00:00
Xidobix
"the first alternative doesn't work with datetimefields." it'd be quite surprising, as datetime.datetime() returns a datetime object http://www.djangoproject.com/documentation/0.96/models/basic/ check the model definition and examples: pub_date = models.DateTimeField() pub_date=datetime(2005, 7, 30)
zalew
"i know it is optional, the problem is that my datetimefield has the time setted, it is not 00:00" Oh, now i get it. Yes, with no time arguments it sets to 00, so it does not return :)
zalew
A: 

See the article Django Documentation

Its very similar to the JZ answer

ur_data_model.objects.filter(ur_date_field=datetime(2005, 7, 27)
Tumbleweed
in the django documentation it works because the datetimefiled has time 00:00
Xidobix
A: 

E.

Model.objects.filter(datetime__year=date.year, datetime__month=date.month, datetime__day=date.day)

prove it to yourself with today() method:

    >>>Model.objects.filter(datetime__year=datetime.date.today().year).filter(datetime__month=datetime.date.today().month).filter(datetime__day=datetime.date.today().day)
skyl
this might actually give you all of the say Thurdays .. ?
skyl
+5  A: 

Such lookups are implmented in django.views.generic.date_based as follows:

{'date_time_field__range': (datetime.datetime.combine(date, datetime.time.min),
                            datetime.datetime.combine(date, datetime.time.max))} 

Because it is quite verbose there are plans to improve the syntax using __date operator. Check "#9596 Comparing a DateTimeField to a date is too hard" for more details.

Piotr Czapla
+1 very helpful answer, thanks!
Van Gale
A: 

This produces the same results as using __year, __month, and __day and seems to work for me:

YourModel.objects.filter(your_datetime_field__startswith=datetime.date(2009,8,22))
mhost
A: 

Hm.. My solution is working:

Mymodel.objects.filter(date_time_field__startswith=datetime.datetime(1986, 7, 28))
satels