Django调试工具栏显示我有一个查询重复1367次

2024-09-27 00:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简短的视图,但是有一个复杂的查询(使用的模型来自数据库MySQL视图)。问题是当这个视图执行时,模板加载太慢。Django调试工具显示查询被复制了1367次,但是重复的查询与复杂的数据库视图查询无关。在

  1. 有没有办法避免重复查询?在
  2. 这个延迟是因为第二个查询吗?在

下面是我的模板和视图代码。很抱歉没有把它们展示得太好,但这是我在StackOverflow上的第一篇文章。在

def simbouloi_list(request, eklid):
    paramorder = request.GET.get('orderoption', '')

    try:
        paramorder = int(paramorder)
    except:
        paramorder = 6  # default ταξινόμηση

    selected_ekloges = Eklogestbl.objects.filter(eklid=eklid)
    # επιλογή όλων των εκλ. αναμετρήσεων με visible=1 και κάνω φθίνουσα ταξινόμηση  αν δεν δοθεί παράμετρος
    all_ekloges = Eklogestbl.objects.filter(visible=1).order_by('-eklid')

    #all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('surname', 'firstname', 'fathername')

    if paramorder==1 or paramorder==6:
        all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('surname', 'firstname','fathername')
    elif paramorder == 2:
        all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('sindiasmos', 'surname', 'firstname','fathername')
    elif paramorder == 3:
        all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('sindiasmos', 'toposeklogis', 'surname', 'firstname','fathername')
    elif paramorder == 4:
        all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by( 'toposeklogis','sindiasmos','surname', 'firstname','fathername')
    else:
        all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('toposeklogis', 'surname','firstname', 'fathername')


    context = {'all_ekloges': all_ekloges,
               'selected_ekloges': selected_ekloges,
               'all_simbouloi': all_simbouloi,
               }

    return render(request, 'Elections/simbouloi_list.html' , context)

模板:

^{pr2}$

Tags: 视图模板byobjectsorderallsurnamefirstname
2条回答

我不认为我可以使用以下命令:

selected_ekloges = all_ekloges.objects.prefetch_related('eklallsimbvw_set').get(eklid=eklid) all_simbouloi = selected_ekloges.eklallsimbvw_set.all()

因为“eklallsimbvw”是由“inspect_db”命令自动创建的模型,它描述的是Mysql视图而不是表,所以这个模型没有与“Eklogestbl”模型相关的外键(我对“prefetch”命令的理解)。在

对于第一个命令,它还会引发一个错误,如“QuerySet”object没有属性“objects”。在

这是我的EklallsimbVw型号:

class EklallsimbVw(models.Model):
id = models.IntegerField(primary_key=True)
eklid = models.IntegerField(db_column='eklID',db_index=True)  # Field name made lowercase.
simbid = models.IntegerField(db_column='simbID',db_index=True)  # Field name made lowercase.
surname = models.CharField(db_column='surname', max_length=100,db_index=True)
firstname = models.CharField(db_column='firstname', max_length=100,db_index=True)
fathername = models.CharField(db_column='fathername', max_length=100,db_index=True)
comments = models.CharField(db_column='comments', max_length=100)
toposeklogisid = models.IntegerField(db_column='toposEklogisID')  # Field name made lowercase.
toposeklogis = models.CharField(db_column='toposEklogis', max_length=100)  # Field name made lowercase.
eidoskoinotitas = models.IntegerField(db_column='eidosKoinotitas', blank=True, null=True)  # Field name made lowercase.
sindid = models.IntegerField(db_column='sindID', blank=True, null=True,db_index=True)  # Field name made lowercase.
sindiasmos = models.CharField(db_column='sindiasmos', max_length=100, blank=True, null=True,db_index=True)

这是“Eklogestbl”模型:

^{pr2}$

试试这个,这是django querysets求值的一个很好的起点https://docs.djangoproject.com/en/2.1/ref/models/querysets/

def simbouloi_list(request, eklid):
 paramorder = 6  # default ταξινόμηση
 all_ekloges = Eklogestbl.objects.filter(visible=1).order_by('-eklid')
 if 'orderoption' in request.GET:
    paramorder = int(request.GET.get('orderoption'))

 selected_ekloges = all_ekloges.objects.prefetch_related('eklalallsimbwv_set').get(eklid=eklid) 
 all_simbouloi = selected_ekloges.eklalallsimbwv_set.all()

 if paramorder==1 or paramorder==6:
    all_simbouloi = all_simbouloi.order_by('surname', 'firstname','fathername') # dosen't hit the database
 elif paramorder == 2:
    all_simbouloi = all_simbouloi.order_by('sindiasmos', 'surname', 'firstname','fathername') # dosen't hit the database
 elif paramorder == 3:
    all_simbouloi = all_simbouloi.order_by('sindiasmos', 'toposeklogis', 'surname', 'firstname','fathername') # dosen't hit the database
elif paramorder == 4:
    all_simbouloi = all_simbouloi.order_by( 'toposeklogis','sindiasmos','surname', 'firstname','fathername') # dosen't hit the database
else:
    all_simbouloi = all_simbouloi.order_by('toposeklogis', 'surname','firstname', 'fathername') # dosen't hit the database


context = {'all_ekloges': all_ekloges,
           'selected_ekloges': selected_ekloges,
           'all_simbouloi': all_simbouloi,
           }

return render(request, 'Elections/simbouloi_list.html' , context)

更新

最后下面的代码工作完美!在

选定的日志=Eklogestbl.objects.prefetch_相关('eklallsimbvw_set').get(eklailsimbvw_集)).get(eklid=eklid)

^{pr2}$

非常感谢你!在

相关问题 更多 >

    热门问题