我有一个根据年级提供课程的教育网站。
GradeLevel 表存储所有可能的 Grade Levels。然后我有 LessonCategories 和 LessonCurriculum 表。
GradeLevel 表与课程表和类别表建立了反向关系。
我遍历 GradeLevel 表中的每个等级(8 个等级),并沿途获取相应的课程和类别。
完成后,我将所有收集到的课程和类别填充到一个列表中,并将其传递给我的模板。
现在,问题是 Django 至少对每个查询进行两次评估。一次用于初始请求,第二次用于我将其放入列表中。(我正在使用 itertools 来链接结果。Itertools 导致查询再次运行。)这会降低我的服务器速度到爬行的不利影响。
我的问题是,是否有人可以查看我的模型和查询,并就更好的查询方式提出建议,以避免和/或减轻这个主要的性能瓶颈。
年级模型:
class GradeLevel(models.Model):
title = models.CharField('Grade',max_length=10, null=True, blank=True, db_index=True)
fullname = models.CharField('Description',max_length=100, null=True, blank=True, db_index=True)
课程类别模型:
class LessonCategory(models.Model):
title = models.CharField(max_length=255, null=True, blank=True, db_index=True)
...
gradelevel = models.ManyToManyField(GradeLevel, related_name='grade_cats', null=True, blank=True)
课程课程:
class LessonCurriculum(models.Model):
title = models.CharField(max_length=255, null=True, blank=True, db_index=True)
...
gradelevel = models.ManyToManyField(GradeLevel, related_name='grade_curriculum', null=True, blank=True)
我的观点:
from itertools import chain
from operator import attrgetter
def my_view(request):
grade_pk = GradeLevel.objects.prefetch_related().get(title='pre-k')
grade_pk_categories = grade_pk.grade_cats.filter(active=True,featured=True)
grade_pk_galleries = grade_pk.grade_curriculum.filter(active=True,featured=True)
grade_k = GradeLevel.objects.prefetch_related().get(title='k')
grade_k_categories = grade_k.grade_cats.filter(active=True,featured=True)
grade_k_galleries = grade_k.grade_curriculum.filter(active=True,featured=True)
grade_1 = GradeLevel.objects.prefetch_related().get(title='1')
grade_1_categories = grade_1.grade_cats.filter(active=True,featured=True)
grade_1_galleries = grade_1.grade_curriculum.filter(active=True,featured=True)
grade_2 = GradeLevel.objects.prefetch_related().get(title='2')
grade_2_categories = grade_2.grade_cats.filter(active=True,featured=True)
grade_2_galleries = grade_2.grade_curriculum.filter(active=True,featured=True)
grade_3 = GradeLevel.objects.prefetch_related().get(title='3')
grade_3_categories = grade_3.grade_cats.filter(active=True,featured=True)
grade_3_galleries = grade_3.grade_curriculum.filter(active=True,featured=True)
grade_4 = GradeLevel.objects.prefetch_related().get(title='4')
grade_4_categories = grade_4.grade_cats.filter(active=True,featured=True)
grade_4_galleries = grade_4.grade_curriculum.filter(active=True,featured=True)
grade_5 = GradeLevel.objects.prefetch_related().get(title='5')
grade_5_categories = grade_5.grade_cats.filter(active=True,featured=True)
grade_5_galleries = grade_5.grade_curriculum.filter(active=True,featured=True)
grade_6 = GradeLevel.objects.prefetch_related().get(title='6')
grade_6_categories = grade_6.grade_cats.filter(active=True,featured=True)
grade_6_galleries = grade_6.grade_curriculum.filter(active=True,featured=True)
grade_7 = GradeLevel.objects.prefetch_related().get(title='7')
grade_7_categories = grade_7.grade_cats.filter(active=True,featured=True)
grade_7_galleries = grade_7.grade_curriculum.filter(active=True,featured=True)
grade_8 = GradeLevel.objects.prefetch_related().get(title='8')
grade_8_categories = grade_8.grade_curriculum.filter(active=True,featured=True)
grade_8_galleries = grade_8.grade_curriculum.filter(active=True,featured=True)
gallery_list = list(set(sorted(chain(grade_pk_categories,grade_pk_galleries,grade_k_categories,grade_k_galleries,grade_1_categories,grade_1_galleries,grade_2_categories,grade_2_galleries,grade_3_categories,grade_3_galleries,grade_4_categories,grade_4_galleries,grade_5_categories,grade_5_galleries,grade_6_categories,grade_6_galleries,grade_7_categories,grade_7_galleries,grade_8_categories,grade_8_galleries), key=attrgetter('display_order'))))