Django模型时间范围过滤方法
我在一个老问题中看到了接下来的两种方法 这里但我不清楚以下之间有什么区别:
{'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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己对此感到困惑,但我想我已经解决了:-DI 发现 有关范围查找选项的文档非常有帮助。
当你这样做时:
SQL 看起来像:
而 django 基于日期的通用视图的这一部分:
变成类似:
生成的 SQL 如下:(
最后一个 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:
The SQL will look something like:
Whereas this part of django's generic date based views:
becomes something like:
which produces SQL along the lines of:
(the format of the timestamp in the last SQL example is wrong obviously, but you get the idea)
Hope that answers your question :)