过滤Django DatetimeField__date不起作用 [英] Filtering django DatetimeField__date not working

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

问题描述

根据在v1上添加的文档.9我们可以按日期查询 DateTimeField ,而无需时间.

According to this document that was added on v1.9 we can able to query a DateTimeField by date without time.

示例为:

Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

但这对我不起作用:

class MilkStorage(models.Model):
    ....

    created_at = models.DateTimeField(null=False)

用法

from datetime import date
MilkStorage.objects.filter(created_at__date=date.today())

它返回一个空的查询集< QuerySet []> .

It returns an empty queryset <QuerySet []>.

此查询仅在 PostgreSQL 上有效吗?我正在使用 MySQL .

Does this query only works on PostgreSQL? im using MySQL.

推荐答案

根据您的特定要求,这可能是理想的解决方案,也可能不是理想的解决方案.我发现,如果在settings.py中将USE_TZ = False设置为__date,则可以使用

Depending on your specific requirements, this may or may not be an ideal solution. I found that __date works if you set USE_TZ = False in settings.py

在禁用USE_TZ之前,我遇到了同样的问题(甚至无法按__month或__day进行过滤).再次,对于您的情况可能不是理想的选择,但是它应该使__date再次起作用.

I had this same problem (I couldn't even filter by __month or __day) until I disabled USE_TZ. Again, may not be ideal for your case, but it should get __date working again.

这篇关于过滤Django DatetimeField__date不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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