获取所有不到一个月的物品 [英] Getting all items less than a month old
本文介绍了获取所有不到一个月的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 django 中获取日期小于一个月前的所有对象.
Is there a way to get all objects with a date less than a month ago in django.
类似于:
items = Item.objects.filter(less than a month old).order_by(...)
推荐答案
您对月"的定义是什么?30天?31天?过去,应该这样做:
What is your definition of a "month"? 30 days? 31 days? Past that, this should do it:
from datetime import datetime, timedelta
last_month = datetime.today() - timedelta(days=30)
items = Item.objects.filter(my_date__gte=last_month).order_by(...)
利用 gte 字段查找的优势.
Takes advantange of the gte field lookup.
这篇关于获取所有不到一个月的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文