rel #713, knock down query count on topic pages

This commit is contained in:
Glen Chiacchieri 2013-04-04 18:45:43 +00:00
parent 066d22cfbb
commit 0f3568466f
2 changed files with 11 additions and 10 deletions

View file

@ -106,13 +106,9 @@
</div>
{% endif %}
{% endif %}
{% with post.attachments.all as attachments %}
{% if attachments %}
{% for attach in attachments %}
<p class="postedit"><em>{% trans "Attachments:" %} <br />{{ attach|attachment_link }}</em></p>
{% endfor %}
{% endif %}
{% endwith %}
{% for attach in posts_with_attachments.post.id %}
<p class="postedit"><em>{% trans "Attachments:" %} <br />{{ attach|attachment_link }}</em></p>
{% endfor %}
</div>
</div>
<div class="clearer"></div>

View file

@ -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,