thebenedict
thebenedict

Reputation: 2589

How to filter by hour in Django 1.6+

I'm trying to filter Performance by start_time hour using Django 1.6. In the console I see :

>>> Performance.objects.first().start_time
datetime.datetime(2014, 4, 6, 11, 0, tzinfo=<UTC>)
>>> Performance.objects.first().start_time.hour
11

But then, from the docs at https://docs.djangoproject.com/en/1.6/ref/models/querysets/#hour:

>>> Performance.objects.filter(start_time__hour=11)
[]

How can I filter by hour? Very similar to this (currently unanswered) question: Django ORM: filter by hour range.

From models.py:

class Performance(models.Model):
    start_time = models.DateTimeField(db_index=True)
    end_time = models.DateTimeField(default=None, blank=True, null=True)
...

Upvotes: 2

Views: 236

Answers (1)

thebenedict
thebenedict

Reputation: 2589

I had to load timezone tables in MYSQL:

http://dev.mysql.com/doc/refman/5.0/en/mysql-tzinfo-to-sql.html

There's a small note in the docs, under https://docs.djangoproject.com/en/1.6/ref/models/querysets/#datetimes

Upvotes: 1

Related Questions