Django模型时间范围过滤方法

发布于 2024-09-05 21:17:46 字数 538 浏览 4 评论 0原文

我在一个老问题中看到了接下来的两种方法 这里但我不清楚以下之间有什么区别:

{'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')

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))}

and

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-09-12 21:17:46

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

当你这样做时:

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 示例中的时间戳格式显然是错误的,但是你明白了)

希望能回答你的问题:)

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.

When you do:

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

The SQL will look something like:

SELECT ... WHERE EXTRACT('year' FROM pub_date) = '2008'
             AND EXTRACT('month' FROM pub_date) = '03'
             AND EXTRACT('day' FROM pub_date) = '27';

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))}

becomes something like:

YourModel.objects.filter(datetime_published__range=(
                datetime.datetime.combine('2008-03-27',datetime.time.min),
                datetime.datetime.combine('2008-03-27',datetime.time.max)
                                                                  )

which produces SQL along the lines of:

SELECT ... WHERE datetime_published BETWEEN '2008-03-27 00:00:00'
                                        AND '2008-03-27 23:59:59';

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

Hope that answers your question :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文