19
有没有办法让所有的对象在django中的日期少于一个月前。获取所有项目小于一个月
喜欢的东西:
items = Item.objects.filter(less than a month old).order_by(...)
有没有办法让所有的对象在django中的日期少于一个月前。获取所有项目小于一个月
喜欢的东西:
items = Item.objects.filter(less than a month old).order_by(...)
什么是你的一个 “月” 的定义是什么? 30天? 31天?过去认为,这应该这样做:
from datetime import datetime, timedelta
last_month = datetime.today() - timedelta(days=30)
items = Item.objects.filter(my_date__gte=last_month).order_by(...)
注意到gte场查找advantange。
items = Item.objects.filter(created_date__gte=aMonthAgo)
其中aMonthAgo按datetime和timedelta计算。
你是什么意思“所有对象”?你是指所有特定类型的物体? – 2009-06-11 05:52:33
是的,类型为“Item”的所有对象 – 2009-06-11 05:56:24