From 0f3568466f21fe2e6381e4d1eb014c25b1380ea9 Mon Sep 17 00:00:00 2001 From: Glen Chiacchieri Date: Thu, 4 Apr 2013 18:45:43 +0000 Subject: [PATCH] rel #713, knock down query count on topic pages --- djangobb_forum/templates/djangobb_forum/topic.html | 10 +++------- djangobb_forum/views.py | 11 ++++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/djangobb_forum/templates/djangobb_forum/topic.html b/djangobb_forum/templates/djangobb_forum/topic.html index 5590060..0268269 100644 --- a/djangobb_forum/templates/djangobb_forum/topic.html +++ b/djangobb_forum/templates/djangobb_forum/topic.html @@ -106,13 +106,9 @@ {% endif %} {% endif %} - {% with post.attachments.all as attachments %} - {% if attachments %} - {% for attach in attachments %} -

{% trans "Attachments:" %}
{{ attach|attachment_link }}

- {% endfor %} - {% endif %} - {% endwith %} + {% for attach in posts_with_attachments.post.id %} +

{% trans "Attachments:" %}
{{ attach|attachment_link }}

+ {% endfor %}
diff --git a/djangobb_forum/views.py b/djangobb_forum/views.py index ec52ae7..f0decf4 100644 --- a/djangobb_forum/views.py +++ b/djangobb_forum/views.py @@ -390,9 +390,13 @@ def show_topic(request, topic_id, full=True): if request.user.is_authenticated(): topic.update_read(request.user) - # without specifying, this query wouldn't select related properly, instead - # it would query: forum_profile, userprofile, forum_topic, forum_attachment - posts = topic.posts.select_related('user__userprofile').all() + # without specifying, following query wouldn't select related properly + posts = topic.posts.select_related('user__userprofile', + 'user__forum_profile', + 'updated_by').all() + # TODO: change the attachments query so it doesn't generate an 'in' clause + posts_with_attachments = {post.id:post.attachments for post in \ + topic.posts.prefetch_related('attachments')} edit_start = timezone.now() - timedelta(minutes=1) edit_end = timezone.now() editable = posts.filter(created__range=(edit_start, edit_end)).filter(user_id=request.user.id) @@ -473,6 +477,7 @@ def show_topic(request, topic_id, full=True): 'moderator': moderator, 'subscribed': subscribed, 'posts': posts, + 'posts_with_attachments': posts_with_attachments, 'first_post_number': first_post_number, 'highlight_word': highlight_word, 'poll': poll,