<?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>Intrepid Blog &#187; select_related</title>
	<atom:link href="http://blog.affien.com/archives/tag/select_related/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:47:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Django annoyances: no reverse select_related</title>
		<link>http://blog.affien.com/archives/2009/05/30/django-annoyances-no-reverse-select_related/</link>
		<comments>http://blog.affien.com/archives/2009/05/30/django-annoyances-no-reverse-select_related/#comments</comments>
		<pubDate>Sat, 30 May 2009 13:48:45 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[select_related]]></category>
		<category><![CDATA[stupid]]></category>

		<guid isPermaLink="false">http://blog.affien.com/?p=380</guid>
		<description><![CDATA[Consider

for page in Page.objects.all():
&#160;&#038;nbs [...]]]></description>
			<content:encoded><![CDATA[<p>Consider</p>
<p><code>for page in Page.objects.all():<br />
&nbsp;&nbsp;print page.title<br />
&nbsp;&nbsp;for comment in page.comments.all():<br />
&nbsp;&nbsp;&nbsp;&nbsp;print comment</code>.</p>
<p>There will be a single query to fetch all pages, but there will be for every page another query to fetch its comments.  Luckily, Django has got a nice trick up its sleave: <code>select_related</code>.  Would I use instead of <code>Page.objects.all()</code>, <code>Page.objects.select_related('comments').all()</code> then Django will use a single joined query to prefetch comments for each page.</p>
<p>However, Django&#8217;s <code>select_related</code> only supports forward one-to-many references.  No many-to-many; certainly no reverce many-to-many; no reverse one-to-many and no, not even reverse one-to-one (<a href="http://code.djangoproject.com/ticket/7270">yet</a>).  <a href="http://code.djangoproject.com/ticket/2238">A developer</a> claims it&#8217;s impossible (which is bullshit), <a href="http://code.djangoproject.com/ticket/9166">another</a> asks for patches, which means he doesn&#8217;t care doing it himself.</p>
<p>It&#8217;s quite easy to manually code around the missing reverse <code>select_related</code>, but it takes too many ugly lines compared to the single word it could&#8217;ve been.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2009/05/30/django-annoyances-no-reverse-select_related/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

