过滤Django DatetimeField

编程入门 行业动态 更新时间:2024-10-27 03:36:42
本文介绍了过滤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

本文发布于:2023-11-22 19:05:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   DatetimeField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!