From b1e27b2023fed8535ed2e3c6b9bbd17fed821636 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Fri, 25 Oct 2024 20:38:19 +0200
Subject: [PATCH] Reduce queries for teachers_and_subjects_table with prefetch

---
 aleksis/apps/alsijil/tasks.py | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/aleksis/apps/alsijil/tasks.py b/aleksis/apps/alsijil/tasks.py
index e8dead026..c7a9ae2b8 100644
--- a/aleksis/apps/alsijil/tasks.py
+++ b/aleksis/apps/alsijil/tasks.py
@@ -3,7 +3,7 @@ from typing import Optional
 from copy import deepcopy
 from datetime import date, timedelta
 
-from django.db.models import Q
+from django.db.models import Q, Prefetch
 from django.utils.translation import gettext as _
 
 from calendarweek import CalendarWeek
@@ -175,12 +175,10 @@ def generate_full_register_printout(
 
     if include_teachers_and_subjects_table:
         group = group.prefetch_related(
-            "courses",
-            "courses__subject",
+            Prefetch("courses", queryset=Course.objects.select_related("subject")),
             "courses__teachers",
             "child_groups",
-            "child_groups__courses",
-            "child_groups__courses__subject",
+            Prefetch("child_groups__courses", queryset=Course.objects.select_related("subject")),
             "child_groups__courses__teachers",
         )
 
-- 
GitLab