获取所有不到一个月大的物品 [英] Getting all items less than a month old
本文介绍了获取所有不到一个月大的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一种方法可以在django中获取日期少于一个月前的所有对象。
items = Item.objects.filter(不到一个月大).order_by(...)
解决方案
你对月的定义是什么? 30天? 31天?过去,这应该这样做:
从$ datetime import datetime,timedelta
last_month = datetime.today() - timedelta(days = 30)
items = Item.objects.filter(my_date__gte = last_month).order_by(...)
充分利用 gte 字段查找。
Is there a way to get all objects with a date less than a month ago in django.
Something like:
items = Item.objects.filter(less than a month old).order_by(...)
解决方案
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(...)
Takes advantange of the gte field lookup.
这篇关于获取所有不到一个月大的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文