Django 查询超过 5 小时的对象的日期时间 [英] Django query datetime for objects older than 5 hours

查看:18
本文介绍了Django 查询超过 5 小时的对象的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为超过 5 小时的小部件编写 Django 查询,但我有点迷茫.小部件模型有一个 DateTimeField,其中填充了小部件的创建时间.

I'm trying to write a Django query for widgets that are more than 5 hours old and I'm a bit lost. The widget model has a DateTimeField that is populated with the creation time of the widget.

推荐答案

如果 Widget 是您的模型的名称,并且它具有名为 created 的 DateTimeField 属性,则查询将是:

If Widget is the name of your model, and it has a DateTimeField attribute named created, the query would be:

from datetime import datetime, timedelta

time_threshold = datetime.now() - timedelta(hours=5)
results = Widget.objects.filter(created__lt=time_threshold)

请注意,created__lt 的意思是创建的小于".

Note that created__lt means "created is less than".

这篇关于Django 查询超过 5 小时的对象的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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