From 644264886c7317bf6ae49fbcedc60e45f6563dca Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Tue, 3 Sep 2019 21:34:42 +0200
Subject: [PATCH] Actually use the prefetched data from the earlier commit.

This method now must be called only with prefetched data (API).
---
 biscuit/apps/chronos/models.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py
index 0242663d..d081812b 100644
--- a/biscuit/apps/chronos/models.py
+++ b/biscuit/apps/chronos/models.py
@@ -130,7 +130,14 @@ class LessonPeriod(SchoolRelated):
 
     def get_substitution(self, week: Optional[int] = None) -> LessonSubstitution:
         wanted_week = week or getattr(self, '_week', None) or current_week()
-        return self.substitutions.filter(week=wanted_week).first()
+
+        # We iterate over all substitutions because this can make use of
+        # prefetching when this model is loaded from outside, in contrast
+        # to .filter()
+        for substitution in self.substitutions.all():
+            if substitution.weeb == wanted_week:
+                return substitution
+        return None
 
     def get_subject(self) -> Optional[Subject]:
         if self.get_substitution():
-- 
GitLab