From 4313304fcd12c9d905d998b3485db2f92644759a Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Fri, 10 Mar 2017 23:18:59 +0100
Subject: [PATCH] Reduce commentaries list view db load

The select_related method reduces the database load
 by a factor of +/- 5 on the list view.
---
 commentaries/views.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/commentaries/views.py b/commentaries/views.py
index 640100a7c..e103063d5 100644
--- a/commentaries/views.py
+++ b/commentaries/views.py
@@ -282,7 +282,9 @@ class CommentaryListView(ListView):
         context = super().get_context_data(**kwargs)
 
         # Get newest comments
-        context['comment_list'] = Comment.objects.vetted().order_by('-date_submitted')[:10]
+        context['comment_list'] = Comment.objects.vetted().select_related(
+                                  'author__user', 'submission', 'commentary').order_by(
+                                  '-date_submitted')[:10]
 
         # Form into the context!
         context['form'] = self.form
-- 
GitLab