我正在使用django,我发现了一个非常令人困惑的问题。在我的views.py中就像这样
if request.method == "GET":
raw_get = request.GET.dict()
raw_data = ''
draw = raw_get.get('draw', 0)
if raw_url == "index":
recordsTotal = Host.objects.count()
raw_data = Host.objects.select_related('belongs_to_ostype').prefetch_related(
'assignedip_set').select_related(
'belongs_to_PlatForm').prefetch_related(
'applications__with_vip').prefetch_related(
'applications__belongs_to_apptype').prefetch_related('applications').all()
start = time()
data = {"data": [i.show_index() for i in raw_data], 'draw': draw, 'recordsTotal': recordsTotal,
'recordsFiltered': recordsTotal}
end = time()
print 'cost time', end - start
return JsonResponse(data)
从终端显示费用时间,如下
cost time 10.1911799908
[21/Oct/2016 09:39:54] "GET /assets/data_index/?_=1477013984767 HTTP/1.1" 200 471123
Howerver,当我使用 python manager.py shell 时,我有了啰嗦
def test2():
recordsTotal = Host.objects.count()
start = time()
data = {"data": [i.show_index() for i in raw_data], 'draw': 0, 'recordsTotal': recordsTotal,'recordsFiltered': recordsTotal}
end = time()
print 'cost time', end - start
此功能与views.py中的功能相同,但成本时间却大不相同。当我运行此功能时,花费时间就像这样
>>> test2()
cost time 4.91849398613
为什么?