Wednesday, May 25, 2011

Querying for Django date ranges

MySQL provides a simple DATE() function that allows you to convert a DATETIME (http://stackoverflow.com/questions/5182275/datetime-equal-or-greater-than-today-in-mysql), but how does one do so in Django?

Apparently, you have use the __range and use datetime.datetime.combine(), as well as datetime.time.min and datetime.time.max to do things:

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

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

`myapp_mytable`.`date` BETWEEN 2011-05-25 00:00:00 and 2011-05-25 23:59:59)'

No comments:

Post a Comment