感到困惑这我自己,但我认为我已经解决了:-DI发现the documentation about the range lookup option非常有帮助。
当你这样做:
YourModel.objects.filter(datetime_published__year='2008',
datetime_published__month='03',
datetime_published__day='27')
的SQL看起来像:
SELECT ... WHERE EXTRACT('year' FROM pub_date) = '2008'
AND EXTRACT('month' FROM pub_date) = '03'
AND EXTRACT('day' FROM pub_date) = '27';
而Django的通用日期为本次此部分:
{'date_time_field__range': (datetime.datetime.combine(date, datetime.time.min),
datetime.datetime.combine(date, datetime.time.max))}
变成类似:
YourModel.objects.filter(datetime_published__range=(
datetime.datetime.combine('2008-03-27',datetime.time.min),
datetime.datetime.combine('2008-03-27',datetime.time.max)
)
沿的线条产生SQL:
SELECT ... WHERE datetime_published BETWEEN '2008-03-27 00:00:00'
AND '2008-03-27 23:59:59';
(在过去的SQL实例的时间戳的格式是错误很明显,但你的想法)
希望这能回答你的问题: )
我预计最后的SQL在大多数数据库上要快得多,因为它可以被优化,所以使用'range'查找可能是首选方法,即使你必须使用“假”开始日期。 – hcalves 2011-11-08 22:34:46