为了账号安全,请及时绑定邮箱和手机立即绑定

如何有效的遍历django的QuerySet

如何有效的遍历django的QuerySet

小怪兽爱吃肉 2018-12-06 10:06:33
如何有效的遍历django的QuerySet
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

def get_employees(position, start, end):
return employee.objects.filter(alert_time__lt=end,alert_time__gt=start).filter(position__in=position)

@login_required
def show(request):
if not validate(request):
return render_to_response('none.html',
context_instance=RequestContext(request, 'msg':'params error')
)

position = request.REQUEST.get('position')
time_range = request.REQUEST.get('time')
start, end = time_range[0], time_range[1]

num_per_page, page_num = get_num(request)
all_employees = get_employees(position, start, end)
# 根据当前页与每页展示的记录数,取到正确的记录
employees = employees_events[(page_num-1)*num_per_page:page_num*num_per_page]

return render_to_response('show_employees.html',
context_instance=RequestContext(
request,
'employees': employees,
'num_per_page': num_per_page,
'page_num':page_num,
'page_options' : [50, 100, 200]
)
)



查看完整回答
反对 回复 2018-12-21
  • 1 回答
  • 0 关注
  • 752 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信