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

问题描述

我正在为超过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 是您的模型的名称,并且具有名为的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".

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

11-02 10:49