# HG changeset patch # User Brian Neal # Date 1256351959 0 # Node ID a20b2c492d55ab2b58acce4a047a980d9b6a9dfd # Parent a3633f39f3ce923c0721c9b4e6ec05dbc6ce43d2 Reduced some sql queries by adding a select_related and monkey patching user profiles onto a user list in shoutbox and the bio/member's list, respectively. diff -r a3633f39f3ce -r a20b2c492d55 gpp/bio/views.py --- a/gpp/bio/views.py Fri Oct 23 03:09:33 2009 +0000 +++ b/gpp/bio/views.py Sat Oct 24 02:39:19 2009 +0000 @@ -35,6 +35,16 @@ except InvalidPage: raise Http404 + # Attach user profiles to each post to avoid using get_user_profile() in + # the template. + users = set(user.id for user in the_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 user in the_page.object_list: + user.user_profile = user_profiles[user.id] + return render_to_response('bio/members.html', { 'page': the_page, 'type': type, diff -r a3633f39f3ce -r a20b2c492d55 gpp/shoutbox/views.py --- a/gpp/shoutbox/views.py Fri Oct 23 03:09:33 2009 +0000 +++ b/gpp/shoutbox/views.py Sat Oct 24 02:39:19 2009 +0000 @@ -52,7 +52,8 @@ def view_history(request, page=1): """This view allows one to view the shoutbox history.""" - paginator = DiggPaginator(Shout.objects.all(), SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2) + paginator = DiggPaginator(Shout.objects.all().select_related(), + SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2) try: the_page = paginator.page(int(page)) except InvalidPage: diff -r a3633f39f3ce -r a20b2c492d55 gpp/templates/bio/members.html --- a/gpp/templates/bio/members.html Fri Oct 23 03:09:33 2009 +0000 +++ b/gpp/templates/bio/members.html Sat Oct 24 02:39:19 2009 +0000 @@ -32,12 +32,12 @@ {% avatar u %} {{ u.username }} {{ u.get_full_name }} - {{ u.get_profile.location }} + {{ u.user_profile.location }} {{ u.date_joined|date:"M. d, Y" }} {% ifnotequal user u %} PM{% endifnotequal %} - {% if not u.get_profile.hide_email %} + {% if not u.user_profile.hide_email %} Email{% endif %}