<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Death of a Gremmie &#187; tips</title>
	<atom:link href="http://deathofagremmie.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://deathofagremmie.com</link>
	<description>by Brian Neal</description>
	<lastBuildDate>Wed, 14 Jul 2010 02:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Django Tip: get_object_or_404() and select_related()</title>
		<link>http://deathofagremmie.com/2009/09/15/get_object_or_404_select_related/</link>
		<comments>http://deathofagremmie.com/2009/09/15/get_object_or_404_select_related/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 00:29:13 +0000</pubDate>
		<dc:creator>gremmie</dc:creator>
				<category><![CDATA[SG101 2.0]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://deathofagremmie.com/?p=301</guid>
		<description><![CDATA[I was looking at the SQL my views were generating, and I came across a couple of places where I was using get_object_or_404(), and then later following some foreign keys in the returned object. Something like this: forum = get_object_or_404(Forum, slug=slug) if not forum.category.can_access(request.user):      return HttpResponseForbidden() The problem is that two SQL queries occur [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking at the SQL my views were generating, and I came across a couple of places where I was using get_object_or_404(), and then later following some foreign keys in the returned object. Something like this:</p>
<pre>
<pre class="brush: python">
forum = get_object_or_404(Forum, slug=slug)
if not forum.category.can_access(request.user):
     return HttpResponseForbidden()
</pre>
</pre>
<p>The problem is that two SQL queries occur here, one during the get_object_or_404(), and then another in the following if statement, when we access category, a foreign key on the forum object. It would sure be nice to somehow use a select_related() there to avoid the extra SQL query. I did some googling, and found a quick tip on one of the <a href="http://michaeltrier.com/2008/3/31/this-week-in-django-16-2008-03-30">This Week in Django podcast pages</a>. And yes, <a href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-object-or-404">the documentation</a> confirmed that get_object_or_404() can now take as a first argument either a model, a manager, or a queryset!</p>
<p>So now you can keep using the handy get_object_or_404() idiom, and reduce the number of queries with a slight bit of refactoring:</p>
<pre>
<pre class="brush: python">
forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
if not forum.category.can_access(request.user):
     return HttpResponseForbidden()
</pre>
</pre>
<p>Very cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://deathofagremmie.com/2009/09/15/get_object_or_404_select_related/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
