问题描述
我正在尝试过滤与日期比较的 DateTimeField
.我的意思是:
I am trying to filter a DateTimeField
comparing with a date. I mean:
MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22))
我得到一个空的查询集列表作为答案,因为(我认为)我没有考虑时间,但我想要任何时间".
I get an empty queryset list as an answer because (I think) I am not considering time, but I want "any time".
在 Django 中有没有一种简单的方法可以做到这一点?
Is there an easy way in Django for doing this?
我设置了日期时间中的时间,不是00:00
.
I have the time in the datetime setted, it is not 00:00
.
推荐答案
此类查找在 django.views.generic.date_based
中实现如下:
Such lookups are implemented in django.views.generic.date_based
as follows:
{'date_time_field__range': (datetime.datetime.combine(date, datetime.time.min),
datetime.datetime.combine(date, datetime.time.max))}
因为它非常冗长,所以计划使用 __date
运算符改进语法.查看#9596 将 DateTimeField 与日期进行比较太难"了解更多详情.
Because it is quite verbose there are plans to improve the syntax using __date
operator. Check "#9596 Comparing a DateTimeField to a date is too hard" for more details.
这篇关于如何在 Django 中过滤 DateTimeField 的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!