11

I have a model with a datetime field:

class MyModel(models.Model):
    created = models.DateTimeField(auto_now = True)

I want to get all the records created today.

I tried:

MyModel.objects.all().filter(created = timezone.now())

and

MyModel.objects.all().filter(created = timezone.now().date())

But always got an empty set. What is the correct way in Django to do this?

EDIT:

It looks strange, but a record, created today (06.04.2012 23:09:44) has date (2012-04-07 04:09:44) in the database. When I'm trying to edit it in the admin panel it looks correct (06.04.2012 23:09:44). Does Django handle it somehow?

Just_Mad
  • 4,029
  • 3
  • 22
  • 30
  • 1
    It sounds like you're running into timezone issues. According to the [documentation](https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/) django store dates with timezone information as UTC in the database. – mklauber Apr 06 '12 at 20:05

3 Answers3

27

Since somewhere in 2015:

YourModel.objects.filter(some_datetime__date=some_date)

i.e. __date after the datetime field.

https://code.djangoproject.com/ticket/9596

MZA
  • 999
  • 11
  • 16
15

There may be a more proper solution, but a quick workup suggests that this would work:

from datetime import timedelta

start_date = timezone.now().date()
end_date = start_date + timedelta( days=1 ) 
Entry.objects.filter(created__range=(start_date, end_date))

I'm assuming timezone is a datetime-like object.

The important thing is that you're storing an exact time, down to the millisecond, and you're comparing it to something that only has accuracy to the day. Rather than toss the hours, minutes, and seconds, django/python defaults them to 0. So if your record is createed at 2011-4-6T06:34:14am, then it compares 2011-4-6T:06:34:14am to 2011-4-6T00:00:00, not 2011-4-6 (from created date) to 2011-4-6 ( from timezone.now().date() ). Helpful?

mklauber
  • 1,126
  • 10
  • 19
0

Try this

from datetime import datetime
now=datetime.now()
YourModel.objects.filter(datetime_published=datetime(now.year, now.month, now.day))
luke14free
  • 2,529
  • 1
  • 17
  • 25
  • 3
    Won't work, you'll still be comparing 2011-04-06TXX:XX:XX to 2011-04-06T00:00:00. This will be false, returning None. – mklauber Apr 06 '12 at 19:43
  • I don't see how it shouldn't work. It's the exact equivalent of "SELECT * FROM table WHERE date='2002-04-05'" – luke14free Apr 06 '12 at 19:47
  • YourModel.objects.filter(created__published=datetime(now.year, now.month, now.day)), am I correct? – Just_Mad Apr 06 '12 at 19:47
  • remove the __published it was just an example of name, use YourModel.objects.filter(created=datetime(now.year, now.month, now.day)) – luke14free Apr 06 '12 at 19:48
  • well, it does not help either. Look at the edit of my question please. Hope this may clear the situation. – Just_Mad Apr 06 '12 at 19:51
  • You have some timezone issues. Implement a datetime.tzinfo class with a utcoffset method and pass it to datetime.now([tz]) as described here http://docs.python.org/library/datetime.html#datetime.tzinfo – luke14free Apr 06 '12 at 20:01
  • @luke14free, the issue is that the dates in the database have seconds on them, so they won't be "equal" to dates without seconds. When python compares dates and datetimes it adds values of zero seconds to dates instead of truncating seconds from datetimes. – mklauber Apr 06 '12 at 20:03
  • @mklauber please, this is not true. If there are no seconds/minutes they are assumed to be zero and my query works. The issue with this guy is better explained in the edit. It's a time lag between the real clock and mysql. If you don't trust me try the query out yourself. – luke14free Apr 06 '12 at 20:04
  • @mklauber btw, do you know about mysql time functions day month and year? – luke14free Apr 06 '12 at 20:06
  • @luke14free, I believe the database contains a record like, `created=2011-04-06T12:53:42` so if you do an equals test to '2011-04-06T00:00:00' (from datetime(...) in the query) they won't be equal. Yes, I'm aware of those, they are exposed in the [year](https://docs.djangoproject.com/en/1.4/ref/models/querysets/#year), month and day selectors in django. – mklauber Apr 06 '12 at 20:13