是否可以执行QuerySet过滤器,该过滤器从指定的,经常性的时间跨度返回对象?
例如,假设相隔几天的开始和结束日期,我希望能够找到:
答案 0 :(得分:1)
未经过测试
对于第一种情况,假设MyModel和您的时间戳被称为日期,您可以这样做:
import datetime
start_date = datetime.date(2012,7,1)
end_date = datetime.date(2012,7,16)
start_time = datetime.time(14,0)
end_time = datetime.time(19,0)
date_range = end_date - start_date
range_list = None
for days in xrange(date_range.days):
d = start_date + datetime.timedelta(days)
if range_list is None:
range_list = Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
else:
range_list = range_list|Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
MyModel.objects.filter(range_list)