From 835782fd7e53a38f81dd867ca40c5abb9aa36fd4 Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 13 Dec 2018 18:15:10 +0100 Subject: [PATCH] forum/views: reduce number of queries with some 'prefetch_related' --- forum/views.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/forum/views.py b/forum/views.py index 94d5ee68..030f39ec 100644 --- a/forum/views.py +++ b/forum/views.py @@ -46,7 +46,7 @@ from core.views import ( ) from core.views.forms import MarkdownInput from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta -from haystack.query import SearchQuerySet +from haystack.query import RelatedSearchQuerySet class ForumSearchView(ListView): @@ -59,12 +59,19 @@ class ForumSearchView(ListView): if query == "": return [] - queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) + queryset = RelatedSearchQuerySet().models(ForumMessage).autocomplete(auto=query) if order_by == "date": queryset = queryset.order_by("-date") queryset = queryset.load_all() + queryset = queryset.load_all_queryset( + ForumMessage, + ForumMessage.objects.all() + .prefetch_related("topic__forum__edit_groups") + .prefetch_related("topic__forum__view_groups") + .prefetch_related("topic__forum__owner_club"), + ) # Filter unauthorized responses resp = []