From 564f041b4a7757efa905352a87f54d747441308e Mon Sep 17 00:00:00 2001 From: Stefan Wehrmeyer Date: Wed, 16 Mar 2022 19:00:44 +0100 Subject: [PATCH] Apply some low-hanging query count reduction --- froide_govplan/cms_plugins.py | 8 +++++--- froide_govplan/models.py | 6 +++--- froide_govplan/views.py | 4 +++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/froide_govplan/cms_plugins.py b/froide_govplan/cms_plugins.py index 72f1035..70f6335 100644 --- a/froide_govplan/cms_plugins.py +++ b/froide_govplan/cms_plugins.py @@ -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 diff --git a/froide_govplan/models.py b/froide_govplan/models.py index 96b31b2..9f4ba50 100644 --- a/froide_govplan/models.py +++ b/froide_govplan/models.py @@ -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: diff --git a/froide_govplan/views.py b/froide_govplan/views.py index 5da4915..a64ef05 100644 --- a/froide_govplan/views.py +++ b/froide_govplan/views.py @@ -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)