在Django中查询datetime到今天的日期 [英] Query datetime by today's date in Django

查看:323
本文介绍了在Django中查询datetime到今天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个对象的数据库中保存 datetime 。我想对数据库进行查询,并从今天 date 而不是datetime中选择任何内容。

I'm saving datetime in the db for an object. I'd like to query against the db and select anything from todays date, not datetime.

最简单的这样做吗?这不起作用:

What's the easiest way to do this? This doesn't work:

invoice_for_today = Invoice.objects.get(user=user, date=date.today())


推荐答案

我记得有计划添加一个 __ date 字段查找以使其更容易,但是因为它是标准的方式。

I remember there being plans to add a __date field lookup to make this easier, but as it stands the "standard" way of doing it is

today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
Invoice.objects.get(user=user, date__range=(today_min, today_max))

这篇关于在Django中查询datetime到今天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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