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> </div>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% with post.attachments.all as attachments %} {% for attach in posts_with_attachments.post.id %}
{% if attachments %} <p class="postedit"><em>{% trans "Attachments:" %} <br />{{ attach|attachment_link }}</em></p>
{% for attach in attachments %} {% endfor %}
<p class="postedit"><em>{% trans "Attachments:" %} <br />{{ attach|attachment_link }}</em></p>
{% endfor %}
{% endif %}
{% endwith %}
</div> </div>
</div> </div>
<div class="clearer"></div> <div class="clearer"></div>

View file

@ -390,9 +390,13 @@ def show_topic(request, topic_id, full=True):
if request.user.is_authenticated(): if request.user.is_authenticated():
topic.update_read(request.user) topic.update_read(request.user)
# without specifying, this query wouldn't select related properly, instead # without specifying, following query wouldn't select related properly
# it would query: forum_profile, userprofile, forum_topic, forum_attachment posts = topic.posts.select_related('user__userprofile',
posts = topic.posts.select_related('user__userprofile').all() '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_start = timezone.now() - timedelta(minutes=1)
edit_end = timezone.now() edit_end = timezone.now()
editable = posts.filter(created__range=(edit_start, edit_end)).filter(user_id=request.user.id) 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, 'moderator': moderator,
'subscribed': subscribed, 'subscribed': subscribed,
'posts': posts, 'posts': posts,
'posts_with_attachments': posts_with_attachments,
'first_post_number': first_post_number, 'first_post_number': first_post_number,
'highlight_word': highlight_word, 'highlight_word': highlight_word,
'poll': poll, 'poll': poll,