# HG changeset patch # User Brian Neal # Date 1256267373 0 # Node ID a3633f39f3ce923c0721c9b4e6ec05dbc6ce43d2 # Parent 19b64e8f02a2091e04d1f5b3eaf7baa7645a8445 Forums: pre-select all the user profiles when displaying a topic. As it was, the template was calling get_user_profile which was causing a db hit on every post in the topic. diff -r 19b64e8f02a2 -r a3633f39f3ce gpp/forums/views.py --- a/gpp/forums/views.py Fri Oct 23 03:08:40 2009 +0000 +++ b/gpp/forums/views.py Fri Oct 23 03:09:33 2009 +0000 @@ -27,6 +27,7 @@ from forums.unread import get_forum_unread_status, get_topic_unread_status, \ get_post_unread_status +from bio.models import UserProfile ####################################################################### TOPICS_PER_PAGE = 50 @@ -109,7 +110,6 @@ topic.save() posts = topic.posts.select_related() - get_post_unread_status(topic, posts, request.user) paginator = create_post_paginator(posts) page_num = int(request.GET.get('page', 1)) @@ -117,6 +117,17 @@ page = paginator.page(page_num) except InvalidPage: raise Http404 + get_post_unread_status(topic, page.object_list, request.user) + + # Attach user profiles to each post to avoid using get_user_profile() in + # the template. + users = set(post.user.id for post in page.object_list) + + profiles = UserProfile.objects.filter(user__id__in=users).select_related() + user_profiles = dict((profile.user.id, profile) for profile in profiles) + + for post in page.object_list: + post.user_profile = user_profiles[post.user.id] last_page = page_num == paginator.num_pages diff -r 19b64e8f02a2 -r a3633f39f3ce gpp/templates/forums/display_post.html --- a/gpp/templates/forums/display_post.html Fri Oct 23 03:08:40 2009 +0000 +++ b/gpp/templates/forums/display_post.html Fri Oct 23 03:09:33 2009 +0000 @@ -6,8 +6,8 @@ {{ post.user.username }}
{% avatar post.user %} Joined: {{ post.user.date_joined|date:"M d, Y" }}
- Posts: {{ post.user.get_profile.forum_post_count }}
- Location: {{ post.user.get_profile.location }}
+ Posts: {{ post.user_profile.forum_post_count }}
+ Location: {{ post.user_profile.location }}