<?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"
	>

<channel>
	<title>Caktus Blog</title>
	<atom:link href="http://www.caktusgroup.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.caktusgroup.com/blog</link>
	<description>Blog &#124; Django Web Development &#124; Raleigh Durham Chapel Hill &#124; Caktus Consulting Group</description>
	<pubDate>Fri, 12 Mar 2010 13:36:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Decoupled Django Apps and the Beauty of Generic Relations</title>
		<link>http://www.caktusgroup.com/blog/2010/03/11/decoupled-django-apps-and-the-beauty-of-generic-relations/</link>
		<comments>http://www.caktusgroup.com/blog/2010/03/11/decoupled-django-apps-and-the-beauty-of-generic-relations/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 21:18:02 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[admin]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[generic relations]]></category>

		<category><![CDATA[open source]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=405</guid>
		<description><![CDATA[Like just about everyone else, we&#8217;ve written our own suite of tools to help with building complex content management systems in Django here at Caktus.  We reviewed a number of the existing CMSes out there, but in almost every case the navigation and page structure were so tightly coupled the system broke down when [...]]]></description>
			<content:encoded><![CDATA[<p>Like just about everyone else, we&#8217;ve written our own suite of tools to help with building complex content management systems in Django here at <a href="http://www.caktusgroup.com/services">Caktus</a>.  We reviewed a number of the existing CMSes out there, but in almost every case the navigation and page structure were so tightly coupled the system broke down when it came time to add additional, non-CMS pages.</p>
<p>We wrote a few little apps, <a href="http://code.google.com/p/django-pagelets/">django-pagelets</a>, <a href="http://code.google.com/p/django-treenav/">django-treenav</a>, and <a href="http://code.google.com/p/django-crumbs/">django-crumbs</a>, each of which manages different pieces of content (little snippets of content, full CMS pages, navigation, and breadcrumbs).  All of the apps are available for free under an open source license on Google Code.</p>
<p>Decoupling was a great move for us, and the ability to plug and play any single part of the system is a huge benefit.  Sometimes, however, the completely decoupled architecture was a bit of a pain: If we didn&#8217;t provide a link from the pagelets app to the treenav app, how would it be possible to edit a page&#8217;s corresponding navigation item on its change form in the Django admin interface?</p>
<p>Enter <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1">Generic Relations</a>.  Using Django&#8217;s content types framework, it&#8217;s possible to create <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline">admin inlines for generic relations</a> with just a few simple lines of code.</p>
<p>In this case, I&#8217;ll show how we allowed users to edit a page&#8217;s corresponding navigation item in django-pagelets without requiring everyone (i.e., those who don&#8217;t need it) to install django-treenav.  First, define the generic inline in the admin.py file of the app that contains the model you want to link to:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">contenttypes</span> <span style="color: #ff7700;font-weight:bold;">import</span> generic
<span style="color: #ff7700;font-weight:bold;">class</span> GenericMenuItemInline<span style="color: black;">&#40;</span>generic.<span style="color: black;">GenericStackedInline</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Add this inline to your admin class to support editing related menu items
    from that model's admin page.
    &quot;&quot;&quot;</span>
    max_num = <span style="color: #ff4500;">1</span>
    model = treenav.<span style="color: black;">MenuItem</span></pre></div></div>

<p>Then, inside the Admin class for the related model in question, dynamically import and add  GenericMenuItemInline to the admin&#8217;s list of inlines based on whether or not it&#8217;s in the project&#8217;s INSTALLED_APPS:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">class</span> PageAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># ...</span>
    inlines = <span style="color: black;">&#91;</span>MyOtherInline<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'treenav'</span> <span style="color: #ff7700;font-weight:bold;">in</span> settings.<span style="color: black;">INSTALLED_APPS</span>:
        <span style="color: #ff7700;font-weight:bold;">from</span> treenav.<span style="color: black;">admin</span> <span style="color: #ff7700;font-weight:bold;">import</span> GenericMenuItemInline
        inlines.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>0, GenericMenuItemInline<span style="color: black;">&#41;</span></pre></div></div>

<p>For more information, see the corresponding <a href="http://code.google.com/p/django-pagelets/source/browse/trunk/pagelets/admin.py">pagelets admin.py</a> and <a href="http://code.google.com/p/django-treenav/source/browse/trunk/treenav/admin.py">treenav admin.py</a>.  Thanks for reading and don&#8217;t hesitate to post comments if you have any questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2010/03/11/decoupled-django-apps-and-the-beauty-of-generic-relations/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Continuous Integration with Django and Hudson CI (Day 1)</title>
		<link>http://www.caktusgroup.com/blog/2010/03/08/django-and-hudson-ci-day-1/</link>
		<comments>http://www.caktusgroup.com/blog/2010/03/08/django-and-hudson-ci-day-1/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 15:58:48 +0000</pubDate>
		<dc:creator>Colin Copeland</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[continuous integration]]></category>

		<category><![CDATA[coverage]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[hudsonci]]></category>

		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=385</guid>
		<description><![CDATA[We&#8217;re always looking for new tools to make our development environment more robust here at Caktus. We write a lot of tests to ensure proper functionality as new features land and bug fixes are added to our projects. The next step is to integrate with a continuous integration system to automate the process and regularly [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re always looking for new tools to make our development environment more robust here at Caktus. We write a lot of <a href="http://www.caktusgroup.com/blog/2009/05/26/testing-django-views-for-concurrency-issues/">tests</a> to ensure proper functionality as new features land and bug fixes are added to our projects. The next step is to integrate with a <a href="http://en.wikipedia.org/wiki/Continuous_integration">continuous integration</a> system to automate the process and regularly check that status of the build.</p>
<p>After attending Dr. C. Titus Brown&#8217;s <a href="http://us.pycon.org/2010/conference/schedule/event/109/">&#8220;Why not run all your tests all the time? A study of continuous integration systems.&#8221;</a> talk at Pycon and seeing <a href="http://hudson.djangoproject.com">Django&#8217;s Hudson setup</a>, I figured I&#8217;d take a look at <a href="http://hudson-ci.org/">Hudson CI</a>.</p>
<h2>Installing Hudson and basic setup</h2>
<p>Hudson is very easy to setup. I started with a fresh Ubuntu 9.10 install on the smallest Rackspace cloud instance and had it running after a few commands. I followed the <a href="http://hudson-ci.org/debian/">Debian</a> setup instructions, which basically consists of:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> - http:<span style="color: #000000; font-weight: bold;">//</span>hudson-ci.org<span style="color: #000000; font-weight: bold;">/</span>debian<span style="color: #000000; font-weight: bold;">/</span>hudson-ci.org.key | <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-key</span> add -
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;deb http://hudson-ci.org/debian binary/&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apt<span style="color: #000000; font-weight: bold;">/</span>sources.list
$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> update
$ <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> hudson
$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

<p>That&#8217;s it! It&#8217;s already up and running on port 8080 using it&#8217;s own web server. Go ahead and pull it up in your browser.</p>
<p>As a test, let&#8217;s setup <a href="http://code.google.com/p/django-crm/">django-crm</a> (a Caktus open-source <a href="http://www.caktusgroup.com/portfolio/community/">community project</a>) as our first Hudson job. Click &#8220;New Job&#8221;, type in a job name, click &#8220;Build a free-style software project&#8221;, and hit OK. django-crm contains a sample project that we&#8217;ll use to run the test suite. On the job configuration page, check Subversion in the Source Code Management section and type in the Repository URL:<br />
<a href="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/hudson-repo.png"><img src="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/hudson-repo.png" alt="" title="hudson-repo" width="500" height="126" class="aligncenter size-full wp-image-387" /></a></p>
<p>Click Save, run the job by clicking &#8220;Build Now&#8221;, and check out the Console Output:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">Started by user anonymous
Checking out a fresh workspace because /var/lib/hudson/jobs/django-crm/workspace/sample_project doesn't exist
Checking out http://django-crm.googlecode.com/svn/trunk/sample_project
A         manage.py
A         site_media
A         site_media/css
A         site_media/css/jquery.autocomplete.css
A         site_media/css/django-contactinfo.css
A         site_media/js
A         site_media/js/jquery-ui-1.7.2.custom.min.js
A         site_media/js/jquery-1.3.2.min.js
A         site_media/js/django-crm.js
A         site_media/js/jquery.autocomplete.min.js
...
Finished: SUCCESS</pre></div></div>

<p>Cool, now let&#8217;s run some tests. Took keep things simple, let&#8217;s grab Django and a few dependencies using aptitude:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>1.1.1<span style="color: #000000; font-weight: bold;">/</span>tarball<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xzvf Django-1.1.1.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> Django-1.1.1
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
$ <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> python-dev python-imaging python-setuptools python-pip</pre></div></div>

<p>To run the tests, add an &#8220;Execute shell&#8221; build step in the Build section with this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -ex</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> sample_project
python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> crm</pre></div></div>

<p>Run the job again and look for the test results in the console output:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">[workspace] $ /bin/sh -xe /tmp/hudson6670261053226891793.sh
+ cd sample_project
+ python manage.py test crm
...
Finished: SUCCESS</pre></div></div>

<h2>XML Test output</h2>
<p>To integrate Hudson with the Django test suite, I used <a href="http://github.com/danielfm/unittest-xml-reporting">unittest-xml-reporting</a>. Just &#8220;pip install unittest-xml-reporting&#8221; and add the following lines to your settings file:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">TEST_RUNNER = <span style="color: #483d8b;">'xmlrunner.extra.djangotestrunner.run_tests'</span>
TEST_OUTPUT_VERBOSE = <span style="color: #008000;">True</span>
TEST_OUTPUT_DESCRIPTIONS = <span style="color: #008000;">True</span>
TEST_OUTPUT_DIR = <span style="color: #483d8b;">'xmlrunner'</span></pre></div></div>

<p>Then check &#8220;Publish JUnit test result report&#8221; in the Post-build Actions section and add the path to the test XML output &#8220;sample_project/xmlrunner/*.xml&#8221;:<br />
<a href="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/test-xml.png"><img src="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/test-xml.png" alt="" title="test-xml" width="331" height="154" class="aligncenter size-full wp-image-388" /></a></p>
<p>Run the job and you should see a new &#8220;Test Result&#8221; link in the navigation. Now you can view the test results right in your browser window.</p>
<h2>Coverage</h2>
<p>To add coverage reports, I used Ned Batchelder&#8217;s <a href="http://nedbatchelder.com/code/coverage/">coverage.py</a> (pip install coverage). Navigate to Hudson&#8217;s plugin manager (Hudson -> Manage Hudson -> Manage Plugins), install the Cobertura Plugin, and restart Hudson when prompted. Then modify your shell script like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -ex</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> sample_project
coverage run manage.py <span style="color: #7a0874; font-weight: bold;">test</span> crm
coverage xml --<span style="color: #007800;">omit</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>This will generate an XML coverage report in the working directory, so we just need to tell Hudson where to look for it. Check &#8220;Publish Cobertura Coverage Report&#8221; in the Post-build Actions section and enter the path to the report:<br />
<a href="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/cobertura.png"><img src="http://www.caktusgroup.com/blog/wp-content/uploads/2010/03/cobertura.png" alt="" title="cobertura" width="310" height="108" class="aligncenter size-full wp-image-389" /></a></p>
<p>Run the build again and you should have access to a new &#8220;Coverage Report&#8221; link. </p>
<h2>More to come&#8230;</h2>
<p>This was just a simple example of getting Hudson setup with a Django project and I know a lot more can be done with Hudson (check out the large number of available plugins). The top items on my todo list are: see Hudson setup environments with virtualenv and pip, integrate more closely with the test suite (possibly using nose), <a href="http://pypi.python.org/pypi/pep8">check for PEP compliance</a>, and setup build failure notifications. I hope to write more as I continue to setup our Hudson environment!</p>
<h3>References</h3>
<p>A few useful Hudson/Python/Django links I discovered while running through this setup:</p>
<ul>
<li><a href="http://guyofgisbourne.blogspot.com/2009/02/django-and-hudson-and-bears-oh-my-and.html">Django and Hudson and Bears, Oh My! (and twill, and figleaf)</a></li>
<li><a href="http://www.rhonabwy.com/wp/2009/11/04/setting-up-a-python-ci-server-with-hudson/">Setting up a python CI server with Hudson</a></li>
<li><a href="http://heisel.org/blog/2009/11/21/django-hudson/">Django continuous integration with Hudson and Nose</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2010/03/08/django-and-hudson-ci-day-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caktus Sends Team of Five to PyCon 2010 in Atlanta</title>
		<link>http://www.caktusgroup.com/blog/2010/02/17/caktus-sends-team-of-five-to-pycon-2010-in-atlanta/</link>
		<comments>http://www.caktusgroup.com/blog/2010/02/17/caktus-sends-team-of-five-to-pycon-2010-in-atlanta/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 18:08:30 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[pycon]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=376</guid>
		<description><![CDATA[Python and Django are tools we use on a daily basis to build fantastic web apps here at Caktus.  I&#8217;m pleased to announce that Caktus is sending five developers&#8211;Colin, Alex, Mike, Mark, and myself&#8211;to PyCon 2010!  PyCon is an annual gathering for users and developers of the open source Python programming language.  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.python.org">Python</a> and <a href="http://www.djangoproject.org">Django</a> are tools we use on a daily basis to build <a href="http://www.caktusgroup.com/services/">fantastic web apps</a> here at Caktus.  I&#8217;m pleased to announce that Caktus is sending five developers&#8211;Colin, Alex, Mike, Mark, and myself&#8211;to <a href="http://us.pycon.org/2010/about/">PyCon 2010</a>!  PyCon is an annual gathering for users and developers of the open source Python programming language.  This year the US conference is being held in Atlanta, GA.  We&#8217;ll be driving down tomorrow (Thursday) from Chapel Hill, NC and staying for the conference weekend plus one day of the sprints.</p>
<p><img src="http://www.processmechanics.com/media/pycon2010/attending-pycon2010-400x60.png" alt="I am attending PyCon"/></p>
<p>Hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2010/02/17/caktus-sends-team-of-five-to-pycon-2010-in-atlanta/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caktus Consulting Group hosts Django sprint in Triangle, NC area</title>
		<link>http://www.caktusgroup.com/blog/2009/12/06/caktus-consulting-group-hosts-django-sprint-in-triangle-nc-area/</link>
		<comments>http://www.caktusgroup.com/blog/2009/12/06/caktus-consulting-group-hosts-django-sprint-in-triangle-nc-area/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 19:37:17 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[open source]]></category>

		<category><![CDATA[sprint]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=369</guid>
		<description><![CDATA[Django is a tool we use every day to build rock-solid web apps here at Caktus, and a development sprint is a concerted, focused period of time in which developers meet in the same space to get things done on a project.
We&#8217;re proud to annouce that Caktus is hosting a local Django development sprint in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.djangoproject.com/">Django</a> is a tool we use every day to build <a href="http://www.caktusgroup.com/services/">rock-solid web apps</a> here at Caktus, and a development sprint is a concerted, focused period of time in which developers meet in the same space to get things done on a project.</p>
<p>We&#8217;re proud to annouce that Caktus is hosting a local Django development sprint in the Triangle (Raleigh, Durham, and Chapel Hill/Carrboro) area of North Carolina.  The sprint will be held the weekend of December 12th and 13th in <a href="http://carrborocoworking.com/">Carrboro Creative Coworking</a>, and the purpose of this sprint will be to help finish features and push out bug fixes in preparation for the upcoming <a href="http://code.djangoproject.com/wiki/Version1.2Features">Django 1.2 release</a>.</p>
<p>If you&#8217;re interested in attending, no previous experience contributing to Django is necessary and the sprint will be a great opportunity to start.  Work on other open source Django-based projects is welcome too.  For more information, check out the <a href="http://code.djangoproject.com/wiki/Sprint200912TriangleNC">corresponding wiki page</a> and don&#8217;t forget to <a href="http://django-triangle-dec-09.eventbrite.com/">register for the event</a>.</p>
<p>We&#8217;ll be there to open the doors at 9am both days.  Courtesy of our <a href="http://code.djangoproject.com/wiki/Sprint200912TriangleNC#Sponsors">sponsors</a> there will be free drinks, snacks, and lunch to go around.  Hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/12/06/caktus-consulting-group-hosts-django-sprint-in-triangle-nc-area/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Custom JOINs with Django&#8217;s query.join()</title>
		<link>http://www.caktusgroup.com/blog/2009/09/28/custom-joins-with-djangos-queryjoin/</link>
		<comments>http://www.caktusgroup.com/blog/2009/09/28/custom-joins-with-djangos-queryjoin/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 15:26:02 +0000</pubDate>
		<dc:creator>Colin Copeland</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[database]]></category>

		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=338</guid>
		<description><![CDATA[Django&#8217;s ORM is great.  It handles simple to fairly complex queries right out the box without having to write any SQL.  If you need a complicated query, Django&#8217;s lets you use .extra(), and you can always fallback to raw SQL if need be, but then you lose the ORM&#8217;s bells and whistles.  [...]]]></description>
			<content:encoded><![CDATA[<p>Django&#8217;s ORM is great.  It handles simple to fairly complex queries right out the box without having to write any SQL.  If you need a complicated query, Django&#8217;s lets you use .extra(), and you can always fallback to raw SQL if need be, but then you lose the ORM&#8217;s bells and whistles.  So it&#8217;s always nice to find solutions that allow you to tap into the ORM at different levels.</p>
<p>Recently, we were looking to perform a LEFT OUTER JOIN through a Many to Many relationship.  For a lack of a better example, let&#8217;s use a Contact model (crm_contact), which has many Phones (crm_phones):</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Contact<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    name = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>
    phones = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Phone'</span><span style="color: black;">&#41;</span>
    addresses = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Address'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Phone<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    number = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span></pre></div></div>

<p>If we want to display each contact and corresponding phone numbers, looping through each contact in Contact.objects.all() and following the phones relationship will generate quite a few database queries (especially with a large contact table).  <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4">select_related()</a> doesn&#8217;t work in this scenario either, because it only supports Foreign Key relationships.  We can use extra() to add a select parameter, but tables=['crm_phones'] will not generate a LEFT OUTER join type. We need to explicitly construct the JOIN.</p>
<p><strong>DISCLAIMER</strong>: The following method does work, but should not be considered best practice.  That is, there may be a better way to accomplish the same task (please comment if so!).  But after sparse Google results for similar scenarios, I figured it&#8217;d at least be useful to post what we discovered.</p>
<p>After digging around in django.db.models.sql for a bit, we found BaseQuery.join in <a href="http://code.djangoproject.com/browser/django/trunk/django/db/models/sql/query.py?rev=10929#L1219">query.py</a>.  Among the possible arguments, the most important is connection, which is &#8220;a tuple (lhs, table, lhs_col, col) where &#8216;lhs&#8217; is either an existing table alias or a table name. The join corresponds to the SQL equivalent of: lhs.lhs_col = table.col&#8221;.  Further, the promote keyword argument will set the join type to be a LEFT OUTER JOIN.</p>
<p>Now we can explicitly setup the JOINs through crm_contact -> crm_contact_phones -> crm_phone:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">contacts = Contact.<span style="color: black;">objects</span>.<span style="color: black;">extra</span><span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">select</span>=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'phone'</span>: <span style="color: #483d8b;">'crm_phone.number'</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#41;</span>.<span style="color: black;">order_by</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># setup intial FROM clause</span>
<span style="color: #808080; font-style: italic;"># OR contacts.query.get_initial_alias()</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #483d8b;">'crm_contact'</span>, <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># join to crm_contact_phones</span>
connection = <span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">'crm_contact'</span>,
    <span style="color: #483d8b;">'crm_contact_phones'</span>,
    <span style="color: #483d8b;">'id'</span>,
    <span style="color: #483d8b;">'contact_id'</span>,
<span style="color: black;">&#41;</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>connection, promote=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># join to crm_phone</span>
connection = <span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">'crm_contact_phones'</span>,
    <span style="color: #483d8b;">'crm_phone'</span>,
    <span style="color: #483d8b;">'phone_id'</span>,
    <span style="color: #483d8b;">'id'</span>,
<span style="color: black;">&#41;</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>connection, promote=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It&#8217;s a little verbose, but it accomplishes our goal.  I used hardcoded table names/columns in the connection tuple to make it easier to follow, but we can also extract this information from the objects themselves:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">contacts = Contact.<span style="color: black;">objects</span>.<span style="color: black;">extra</span><span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">select</span>=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'phone'</span>: <span style="color: #483d8b;">'crm_phone.number'</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#41;</span>.<span style="color: black;">order_by</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># setup intial FROM clause</span>
<span style="color: #808080; font-style: italic;"># OR contacts.query.get_initial_alias()</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, Contact._meta.<span style="color: black;">db_table</span>, <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># join to crm_contact_phones</span>
connection = <span style="color: black;">&#40;</span>
    Contact._meta.<span style="color: black;">db_table</span>, <span style="color: #808080; font-style: italic;"># crm_contact</span>
    Contact.<span style="color: black;">phones</span>.<span style="color: black;">field</span>.<span style="color: black;">m2m_db_table</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #808080; font-style: italic;"># crm_contact_phones</span>
    Contact._meta.<span style="color: black;">pk</span>.<span style="color: black;">column</span>, <span style="color: #808080; font-style: italic;"># etc...</span>
    Contact.<span style="color: black;">phones</span>.<span style="color: black;">field</span>.<span style="color: black;">m2m_column_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>connection, promote=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># join to crm_phone</span>
connection = <span style="color: black;">&#40;</span>
    Contact.<span style="color: black;">phones</span>.<span style="color: black;">field</span>.<span style="color: black;">m2m_db_table</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
    Phone._meta.<span style="color: black;">db_table</span>,
    Contact.<span style="color: black;">phones</span>.<span style="color: black;">field</span>.<span style="color: black;">m2m_reverse_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
    Phone._meta.<span style="color: black;">pk</span>.<span style="color: black;">column</span>,
<span style="color: black;">&#41;</span>
contacts.<span style="color: black;">query</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>connection, promote=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This results in a row for each phone number (Cartesian product), but we can print out each contact and corresponding phone numbers (with a single SQL statement) quickly in a template using <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ifchanged">{% ifchanged %}</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h1&gt;Contacts&lt;/h1&gt;
&nbsp;
{% for contact in contacts %}
    {% ifchanged contact.name %}
        &lt;h2&gt;{{ contact.name }}&lt;/h2&gt;
    {% endifchanged %}
    &lt;p&gt;Phone: {{ contact.phone }}&lt;/p&gt;
{% endfor %}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/09/28/custom-joins-with-djangos-queryjoin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Web Developer for Hire</title>
		<link>http://www.caktusgroup.com/blog/2009/09/23/developer-for-hire/</link>
		<comments>http://www.caktusgroup.com/blog/2009/09/23/developer-for-hire/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 15:26:58 +0000</pubDate>
		<dc:creator>Colin Copeland</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[job]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=317</guid>
		<description><![CDATA[We&#8217;re pleased to announce that Caktus is looking for a developer to join our team on a contract basis!
What do we do? We build custom web applications for local and remote clients using a variety of open-source technologies.  We are a small team founded in the Chapel Hill/Carrboro area (currently residing in Carrboro Creative [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re pleased to announce that <a href="http://www.caktusgroup.com/">Caktus</a> is looking for a developer to join our team on a contract basis!</p>
<p>What do we do? We build custom web applications for local and remote clients using a variety of open-source technologies.  We are a small team founded in the Chapel Hill/Carrboro area (currently residing in <a href="http://www.carrborocoworking.com/">Carrboro Creative Coworking</a>) who believe in face-to-face contact and employ agile development techniques that emphasize teamwork and collaboration.</p>
<p>We&#8217;re looking for a strong software developer who enjoys working on a team and is excited to learn and experiment with new technologies.  We do have a preference for local candidates, but will consider all submissions.  Initial work will focus on maintaining small <a href="http://www.djangoproject.com">Django</a>-powered websites.  This will involve HTML/CSS (including converting Photoshop designs), Django Templates, and writing Unit Tests.  Later work will involve creating and integrating Django apps into larger projects, deployment, and database work.</p>
<p>You will be working in Linux (Debian-flavor) production environments with Apache and WSGI.  Python/Django experience is not required, but will be used on a daily basis.  Relational database experience is a must.  HTML/CSS and JavaScript experience are also a must, and jQuery is a plus.</p>
<p>If you&#8217;re interested in this position, please <a href="mailto:jobs+caktus@caktusgroup.com?subject=Developer for Hire">send us</a> your resume, some example code, links to any open-source projects you&#8217;ve contributed to, and expected compensation.  We&#8217;re excited to bring on a new team member!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/09/23/developer-for-hire/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Open Source Django Projects from Caktus Consulting Group</title>
		<link>http://www.caktusgroup.com/blog/2009/09/07/open-source-django-apps-from-caktus-consulting-group/</link>
		<comments>http://www.caktusgroup.com/blog/2009/09/07/open-source-django-apps-from-caktus-consulting-group/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 00:48:44 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=304</guid>
		<description><![CDATA[At Caktus we&#8217;re big fans of reusing code.  We leverage many open source projects&#8211;especially Django apps&#8211;to accomplish a variety of tasks.  In addition, we&#8217;ve written quite a few pluggable apps over the paste two years that we reuse over and over again for different projects.  As a way of giving back to [...]]]></description>
			<content:encoded><![CDATA[<p>At Caktus we&#8217;re big fans of reusing code.  We leverage many open source projects&#8211;especially Django apps&#8211;to accomplish a variety of tasks.  In addition, we&#8217;ve written quite a few pluggable apps over the paste two years that we reuse over and over again for different projects.  As a way of giving back to the community, we&#8217;ve polished and released a portion of that code as open source ourselves.  While some of the projects have been available on Google Code for awhile now, we just put together a consolidated list of <a href="http://www.caktusgroup.com/portfolio/community/">open source Django projects</a> on our web site to serve as a jumping off point for all the projects we like, we contributed to, and we created.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/09/07/open-source-django-apps-from-caktus-consulting-group/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caktus Consulting Group, LLC sponsors DjangoCon 2009</title>
		<link>http://www.caktusgroup.com/blog/2009/09/05/djangocon-2009/</link>
		<comments>http://www.caktusgroup.com/blog/2009/09/05/djangocon-2009/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 04:58:29 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[djangocon]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=295</guid>
		<description><![CDATA[Django is a tool we use on a daily basis to build fantastic web apps here at Caktus, and DjangoCon is the annual conference for Django developers and other community members.  We are proud to announce that Caktus Consulting Group, LLC is sponsoring DjangoCon 2009!
This year, the conference is being held the week of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.djangoproject.com/">Django</a> is a tool we use on a daily basis to build <a href="http://www.caktusgroup.com/services/">fantastic web apps</a> here at Caktus, and <a href="http://www.djangocon.org">DjangoCon</a> is the annual conference for Django developers and other community members.  We are proud to announce that Caktus Consulting Group, LLC is sponsoring DjangoCon 2009!</p>
<p>This year, the conference is being held the week of September 7th in the beautiful city of Portland, Oregon.  Two Caktus partners, Colin and myself, will be attending. We hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/09/05/djangocon-2009/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating recursive, symmetrical many-to-many relationships in Django</title>
		<link>http://www.caktusgroup.com/blog/2009/08/14/creating-recursive-symmetrical-many-to-many-relationships-in-django/</link>
		<comments>http://www.caktusgroup.com/blog/2009/08/14/creating-recursive-symmetrical-many-to-many-relationships-in-django/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 19:10:43 +0000</pubDate>
		<dc:creator>tobias</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[database]]></category>

		<category><![CDATA[django]]></category>

		<category><![CDATA[models]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=282</guid>
		<description><![CDATA[In Django, a recursive many-to-many relationship is a ManyToManyField that points to the same model in which it&#8217;s defined (&#8217;self&#8217;).  A symmetrical relationship is one in where, when a.contacts = [b], a is in b.contacts.
In changeset 8136, support for through models was added to the Django core.  This allows you to create a [...]]]></description>
			<content:encoded><![CDATA[<p>In Django, a recursive many-to-many relationship is a <code>ManyToManyField</code> that points to the same model in which it&#8217;s defined (&#8217;self&#8217;).  A symmetrical relationship is one in where, when <code>a.contacts = [b]</code>, <code>a is in b.contacts</code>.</p>
<p>In <a href="http://code.djangoproject.com/changeset/8136">changeset 8136</a>, support for <code>through</code> models was added to the Django core.  This allows you to create a many-to-many relationship that goes through a model of your choice:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Contact<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    contacts = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">'self'</span>,
        through=<span style="color: #483d8b;">'ContactRelationship'</span>,
        symmetrical=<span style="color: #008000;">False</span>,
    <span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ContactRelationship<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">types</span> = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">'RelationshipType'</span>,
        related_name=<span style="color: #483d8b;">'contact_relationships'</span>,
        blank=<span style="color: #008000;">True</span>,
    <span style="color: black;">&#41;</span>
    from_contact = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Contact'</span>, related_name=<span style="color: #483d8b;">'from_contacts'</span><span style="color: black;">&#41;</span>
    to_contact = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Contact'</span>, related_name=<span style="color: #483d8b;">'to_contacts'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        unique_together = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'from_contact'</span>, <span style="color: #483d8b;">'to_contact'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>According to the <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships">Django Docs</a>, you must set <code>symmetrical=False</code> for recursive many-to-many relationships.  Sometimes&#8211;for a recent case in <a href="http://code.google.com/p/django-crm">django-crm</a>, for example&#8211;what you really want is a <strong>symmetrical, recursive many-to-many relationship.</strong></p>
<p>The trick to getting this working is understanding what <code>symmetrical=True</code> actually does.  From what we can tell after a brief look through the Django core, <code>symmetrical=True</code> is simply a utility that (a) creates a second, reverse relationship in the many-to-many table, and (b) hides the field in the related model (in this case the same model) from use by appending a &#8216;+&#8217; to its name.</p>
<p>Since you normally have to create many-to-many relationships manually when a <code>through</code> model is specified, the solution is simply to leave <code>symmetrical=False</code> (otherwise it&#8217;ll raise an exception) and create the reverse relationship manually yourself via the <code>through</code> model:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">crm.<span style="color: black;">ContactRelationship</span>.<span style="color: black;">objects</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span>
    from_contact=contact_a,
    to_contact=contact_b,
<span style="color: black;">&#41;</span>
crm.<span style="color: black;">ContactRelationship</span>.<span style="color: black;">objects</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span>
    from_contact=contact_b,
    to_contact=contact_a,
<span style="color: black;">&#41;</span></pre></div></div>

<p>Additionally, you&#8217;ll have to do a little cleanup to make sure both sides of the relationship are removed when one is removed, but otherwise this should achieve the same effect as setting <code>symmetrical=True</code> in other many-to-many relationships.</p>
<p>To hide the other side of the related manager, you can append a &#8216;+&#8217; to the related_name, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Contact<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    contacts = models.<span style="color: black;">ManyToManyField</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">'self'</span>,
        through=<span style="color: #483d8b;">'ContactRelationship'</span>,
        symmetrical=<span style="color: #008000;">False</span>,
        related_name=<span style="color: #483d8b;">'related_contacts+'</span>,
    <span style="color: black;">&#41;</span></pre></div></div>

<p>Good luck and feel free to comment with any questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/08/14/creating-recursive-symmetrical-many-to-many-relationships-in-django/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Setting PostgreSQL&#8217;s SHMMAX in Mac OS X 10.5 (Leopard)</title>
		<link>http://www.caktusgroup.com/blog/2009/08/13/setting-postgresqls-shmmax-in-mac-os-x-105-leopard/</link>
		<comments>http://www.caktusgroup.com/blog/2009/08/13/setting-postgresqls-shmmax-in-mac-os-x-105-leopard/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 12:46:08 +0000</pubDate>
		<dc:creator>Colin Copeland</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[macosx]]></category>

		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.caktusgroup.com/blog/?p=272</guid>
		<description><![CDATA[If you&#8217;ve ever tried to increase the shared_buffers setting in your postgresql.conf to a value that exceeds the amount of shared memory supported by your operating system kernel, then you&#8217;ll see an error message like this:

copelco@montgomery:~$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data
2009-07-10 10:14:04 EDTFATAL:  could not create shared memory segment: Invalid argument
2009-07-10 10:14:04 EDTDETAIL:  Failed system [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever tried to increase the <a href="http://www.postgresql.org/docs/current/static/runtime-config-resource.html#GUC-SHARED-BUFFERS">shared_buffers</a> setting in your postgresql.conf to a value that exceeds the amount of shared memory supported by your operating system kernel, then you&#8217;ll see an error message like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>postmaster <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>data
<span style="color: #000000;">2009</span>-07-<span style="color: #000000;">10</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">14</span>:04 EDTFATAL:  could not create shared memory segment: Invalid argument
<span style="color: #000000;">2009</span>-07-<span style="color: #000000;">10</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">14</span>:04 EDTDETAIL:  Failed system call was shmget<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">key</span>=<span style="color: #000000;">5432001</span>, <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">size</span></span>=<span style="color: #000000;">142516224</span>, 03600<span style="color: #7a0874; font-weight: bold;">&#41;</span>.
<span style="color: #000000;">2009</span>-07-<span style="color: #000000;">10</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">14</span>:04 EDTHINT:  This error usually means that PostgreSQL<span style="color: #ff0000;">'s request for a shared memory segment exceeded your kernel'</span>s SHMMAX parameter.  You can either reduce the request <span style="color: #c20cb9; font-weight: bold;">size</span> or reconfigure the kernel with larger SHMMAX.  To reduce the request <span style="color: #c20cb9; font-weight: bold;">size</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>currently <span style="color: #000000;">142516224</span> bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>, reduce PostgreSQL<span style="color: #ff0000;">'s shared_buffers parameter (currently 16384) and/or its max_connections parameter (currently 23).
	If the request size is already small, it'</span>s possible that it is <span style="color: #c20cb9; font-weight: bold;">less</span> than your kernel<span style="color: #ff0000;">'s SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.
	The PostgreSQL documentation contains more information about shared memory configuration.</span></pre></div></div>

<p>The shared_buffers default value is low (for legacy reasons).  If you increase it, PostgreSQL may request a shared memory segment that exceeds your kernel&#8217;s SHMMAX paramter.  You can see the current values like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ sysctl kern.sysv.shmmax
kern.sysv.shmmax: <span style="color: #000000;">4194304</span>
copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ sysctl kern.sysv.shmall
kern.sysv.shmall: <span style="color: #000000;">1024</span></pre></div></div>

<p><a href="http://www.postgresql.org/docs/current/static/kernel-resources.html">17.4. Managing Kernel Resources</a> outlines methods to set the values permanently, but you can play around with the values temporarily (until restart) on the command line like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> sysctl <span style="color: #660033;">-w</span> kern.sysv.shmmax=<span style="color: #000000;">1073741824</span>
kern.sysv.shmmax: <span style="color: #000000;">4194304</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1073741824</span>
copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> sysctl <span style="color: #660033;">-w</span> kern.sysv.shmall=<span style="color: #000000;">1073741824</span>
kern.sysv.shmall: <span style="color: #000000;">1024</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1073741824</span></pre></div></div>

<p>Once you have working values, you can fire up PostgreSQL (I&#8217;ve been happy with the <a href="http://www.kyngchaos.com/software:postgres">kyngchaos</a> distribution) with a LaunchDaemon file and launchd:</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Label<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.postgresql.postgres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ProgramArguments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/bin/postmaster<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-D<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RunAtLoad<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UserName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>copelco<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And the launchd commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl unload <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>org.postgresql.postgres.plist
copelco<span style="color: #000000; font-weight: bold;">@</span>montgomery:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl load <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>org.postgresql.postgres.plist</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.caktusgroup.com/blog/2009/08/13/setting-postgresqls-shmmax-in-mac-os-x-105-leopard/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
