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 (
PLUGIN_TEMPLATES,
GovernmentPlanSection,
GovernmentPlansCMSPlugin,
GovernmentPlanSection,
GovernmentPlanSectionsCMSPlugin,
)
@ -39,13 +39,15 @@ class GovernmentPlanSectionsPlugin(CMSPluginBase):
def render(self, context, instance, placeholder):
context = super().render(context, instance, placeholder)
if instance.government:
if instance.government_id:
sections = GovernmentPlanSection.objects.filter(
government=instance.government
government_id=instance.government_id
)
else:
sections = GovernmentPlanSection.objects.all()
sections = sections.select_related("government")
context["sections"] = sections
return context

View file

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

View file

@ -27,7 +27,9 @@ class GovPlanSectionDetailView(GovernmentMixin, DetailView):
template_name = "froide_govplan/section.html"
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):
context = super().get_context_data(**kwargs)