forum/views: reduce number of queries with some 'prefetch_related'

This commit is contained in:
Skia 2018-12-13 18:15:10 +01:00
parent 82df424146
commit 835782fd7e

View File

@ -46,7 +46,7 @@ from core.views import (
) )
from core.views.forms import MarkdownInput from core.views.forms import MarkdownInput
from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta
from haystack.query import SearchQuerySet from haystack.query import RelatedSearchQuerySet
class ForumSearchView(ListView): class ForumSearchView(ListView):
@ -59,12 +59,19 @@ class ForumSearchView(ListView):
if query == "": if query == "":
return [] return []
queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) queryset = RelatedSearchQuerySet().models(ForumMessage).autocomplete(auto=query)
if order_by == "date": if order_by == "date":
queryset = queryset.order_by("-date") queryset = queryset.order_by("-date")
queryset = queryset.load_all() 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 # Filter unauthorized responses
resp = [] resp = []