Django模型计时器过滤方法 [英] Django model timerange filtering method

查看:159
本文介绍了Django模型计时器过滤方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在旧问题中看到接下来的两种方法这里,但不清楚我有什么区别:

I saw the next two methods in an old question here but it is not clear for me what is the difference between:

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

YourModel.objects.filter(datetime_published__year='2008', 
                     datetime_published__month='03', 
                     datetime_published__day='27')


推荐答案

对我自己感到困惑,但我想我已经解决了:-DI found 有关范围查找选项的文档非常有帮助。

Was confused about this myself, but I think I've worked it out :-D I found the documentation about the range lookup option very helpful.

当你做:

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的基于日期的通用视图:

Whereas this part of django's generic date based views:

{'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示例中的时间戳的格式为w很明显,但你得到这个想法)

(the format of the timestamp in the last SQL example is wrong obviously, but you get the idea)

希望回答你的问题:)

这篇关于Django模型计时器过滤方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆