Apply some low-hanging query count reduction

This commit is contained in:
Stefan Wehrmeyer 2022-03-16 19:00:44 +01:00
parent ff866fa052
commit 564f041b4a
3 changed files with 11 additions and 7 deletions

View file

@ -5,8 +5,8 @@ from cms.plugin_pool import plugin_pool
from .models import ( from .models import (
PLUGIN_TEMPLATES, PLUGIN_TEMPLATES,
GovernmentPlanSection,
GovernmentPlansCMSPlugin, GovernmentPlansCMSPlugin,
GovernmentPlanSection,
GovernmentPlanSectionsCMSPlugin, GovernmentPlanSectionsCMSPlugin,
) )
@ -39,13 +39,15 @@ class GovernmentPlanSectionsPlugin(CMSPluginBase):
def render(self, context, instance, placeholder): def render(self, context, instance, placeholder):
context = super().render(context, instance, placeholder) context = super().render(context, instance, placeholder)
if instance.government: if instance.government_id:
sections = GovernmentPlanSection.objects.filter( sections = GovernmentPlanSection.objects.filter(
government=instance.government government_id=instance.government_id
) )
else: else:
sections = GovernmentPlanSection.objects.all() sections = GovernmentPlanSection.objects.all()
sections = sections.select_related("government")
context["sections"] = sections context["sections"] = sections
return context return context

View file

@ -411,7 +411,7 @@ class GovernmentPlanSection(models.Model):
def get_plans(self): def get_plans(self):
return ( return (
GovernmentPlan.objects.filter( GovernmentPlan.objects.filter(
categories__in=self.categories.all(), government=self.government categories__in=self.categories.all(), government_id=self.government_id
) )
.distinct() .distinct()
.order_by("title") .order_by("title")
@ -486,8 +486,8 @@ if CMSPlugin:
plans = GovernmentPlan.objects.all() plans = GovernmentPlan.objects.all()
filters = {} filters = {}
if self.government: if self.government_id:
filters["government"] = self.government filters["government_id"] = self.government_id
cat_list = self.categories.all().values_list("id", flat=True) cat_list = self.categories.all().values_list("id", flat=True)
if cat_list: if cat_list:

View file

@ -27,7 +27,9 @@ class GovPlanSectionDetailView(GovernmentMixin, DetailView):
template_name = "froide_govplan/section.html" template_name = "froide_govplan/section.html"
def get_queryset(self): def get_queryset(self):
return GovernmentPlanSection.objects.filter(government=self.government) return GovernmentPlanSection.objects.filter(
government=self.government
).select_related("government")
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)