根据在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
发布评论