我有一些 django 代码如下。我只是过滤不同的对象并尝试获取 fitler 结果的总数。然后将这些数字传递给 html 页面。在 html 页面中,我只显示这些总数。就这样。
但是,当用户访问此页面时,速度确实很慢。我看不到任何可以加快程序速度的代码改进。有没有人对加速代码有任何建议,或者我所能做的就是升级服务器?
我正在考虑在数据库中创建这些表的视图或索引。但是我对如何在 django 中做到这一点没有理想。我也可以直接在数据库中进行操作,但是如何从 django 访问数据库中的视图?
try:
p_r = P.objects.get(p_id=rec_id, f__f_id=f_id, d__n=d)
s = S.objects.get(r=p_r)
except S.DoesNotExist:
s = None
except S.MultipleObjectsReturned:
s = S.objects.filter(r=p_r)
try:
b = B.objects.filter(r=p_r)
except B.DoesNotExist:
b = None
bu = {}
if b != None and len(b) > 0:
bu['count'] = len(b)
try:
a = A.objects.filter(r=p_r)
except A.DoesNotExist:
a = None
an = {}
if a != None and len(a) > 0:
an['count'] = len(a)
try:
ar = AR.objects.filter(r=p_r)
except AR.DoesNotExist:
ar = None
ad = {}
if ar != None and len(ar) > 0:
ad['count'] = len(ar)
try:
c = C.objects.filter(r=p_r)
except C.DoesNotExist:
c = None
co ={}
if c != None and len(c) > 0:
co['count'] = len(c)
try:
p_e = []
ev = E.objects.all()
for e in ev:
if e.r_o.p_id == rec_id and e.r_o.record.f.f_id == f_id:
patient_events.append(e)
except E.DoesNotExist:
ev = None
ph = {}
if p_e and len(p_e) > 0:
ph['count'] = len(p_e)
Log().add(request, "View", "I", 'pr', p_r.id)
response_dict.update ({'record': p_r,
'summary': s,
'bu': bu,
'an': an,
'ad': ad,
'co': co,
'ph': ph,
'p_id': rec_id,
'f_id': f_id,
'd': d_id,
})
return render_to_response('records/detail.html',response_dict, context_instance=RequestContext(request))