From c375bf5befb3ee6a212db859a5e12d8121f35501 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sun, 20 Feb 2022 22:03:00 +0100
Subject: [PATCH] Further accelerations for organization detail page

---
 scipost_django/organizations/models.py | 8 +++++---
 scipost_django/organizations/views.py  | 4 +++-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/scipost_django/organizations/models.py b/scipost_django/organizations/models.py
index 85a17595b..8dc4a6dfc 100644
--- a/scipost_django/organizations/models.py
+++ b/scipost_django/organizations/models.py
@@ -206,9 +206,11 @@ class Organization(models.Model):
         Returns all Profiles of authors associated to this Organization.
         """
         profile_id_list = [
-            tbl.profile.id for tbl in self.publicationauthorstable_set.all()
+            tbl.profile.id for tbl in self.publicationauthorstable_set.all(
+            ).select_related("profile")
         ]
-        return Profile.objects.filter(id__in=profile_id_list).distinct()
+        return Profile.objects.filter(id__in=profile_id_list).distinct(
+        ).select_related("contributor")
 
     def fellowships(self, year=None):
         """
@@ -222,7 +224,7 @@ class Organization(models.Model):
                 Q(date_from__isnull=True) | Q(date_from__year__lte=year),
                 Q(date_until__isnull=True) | Q(date_until__year__gte=year),
             )
-        profile_ids = [a.profile.id for a in affiliations]
+        profile_ids = [a.profile.id for a in affiliations.select_related("profile")]
         fellowships = Fellowship.objects.filter(
             contributor__profile__id__in=profile_ids
         )
diff --git a/scipost_django/organizations/views.py b/scipost_django/organizations/views.py
index fe9208caf..6670a4ae6 100644
--- a/scipost_django/organizations/views.py
+++ b/scipost_django/organizations/views.py
@@ -196,10 +196,12 @@ class OrganizationDetailView(DetailView):
         if not self.request.user.has_perm("scipost.can_manage_organizations"):
             queryset = queryset.exclude(orgtype=ORGTYPE_PRIVATE_BENEFACTOR)
         # return queryset
-        return queryset.prefetch_related(
+        return queryset.select_related("parent").prefetch_related(
             "children",
             "subsidy_set",
+            "contactperson_set",
             "contactrole_set",
+            "funder_set",
             "organizationevent_set",
             "pubfractions",
         )
-- 
GitLab