<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Collin Grady</title>
	<atom:link href="http://collingrady.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://collingrady.wordpress.com</link>
	<description></description>
	<lastBuildDate>Wed, 07 Jan 2009 06:44:52 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/9b302b8d4205b846e4930474d6314d84?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Collin Grady</title>
		<link>http://collingrady.wordpress.com</link>
	</image>
			<item>
		<title>mod_python versus mod_wsgi</title>
		<link>http://collingrady.wordpress.com/2009/01/06/mod_python-versus-mod_wsgi/</link>
		<comments>http://collingrady.wordpress.com/2009/01/06/mod_python-versus-mod_wsgi/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 03:09:29 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://collingrady.wordpress.com/?p=123</guid>
		<description><![CDATA[So at work recently, we started looking at switching our django setups from mod_python to mod_wsgi, as we kept having memory and cpu use issues with mod_python, and mod_wsgi was supposed to be far better with performance.
Well, it&#8217;s only been a day so far, but the results have been very promising. Our memory usage was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=123&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So at <a title="Chesspark" href="http://www.chesspark.com/">work</a> recently, we started looking at switching our django setups from mod_python to mod_wsgi, as we kept having memory and cpu use issues with mod_python, and mod_wsgi was supposed to be far better with performance.</p>
<p>Well, it&#8217;s only been a day so far, but the results have been very promising. Our memory usage was cut in half, and load bottomed out.</p>
<p><em>Edit:</em> this is daemon mode with threads=1</p>
<p>Initial testing showed more drastic improvements, so for anyone else testing, take initial results with a grain of salt <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>With that said, here are the actual results:</p>
<p>Just after switching:</p>
<p><a href="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png"><img class="alignnone size-full wp-image-124" style="border-style:dotted;" title="mod_python to mod_wsgi" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png?w=550&#038;h=94" alt="mod_python to mod_wsgi" width="550" height="94" /></a></p>
<p>A day after switching:</p>
<p><a href="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png"><img class="alignnone size-full wp-image-125" style="border-style:dotted;" title="mod_python-to-mod_wsgi-day-after" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png?w=550&#038;h=92" alt="mod_python-to-mod_wsgi-day-after" width="550" height="92" /></a></p>
<p>Too early to be absolutely sure that everything is working well, but it&#8217;s all looking good so far <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img style="display:none;" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png" alt="" /><img style="display:none;" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png" alt="" /></p>
<p><img style="display:none;" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png" alt="" /><img style="display:none;" src="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png" alt="" /></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=123&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2009/01/06/mod_python-versus-mod_wsgi/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png" medium="image">
			<media:title type="html">mod_python to mod_wsgi</media:title>
		</media:content>

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png" medium="image">
			<media:title type="html">mod_python-to-mod_wsgi-day-after</media:title>
		</media:content>

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png" medium="image" />

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png" medium="image" />

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi.png" medium="image" />

		<media:content url="http://collingrady.files.wordpress.com/2009/01/mod_python-to-mod_wsgi-day-after.png" medium="image" />
	</item>
		<item>
		<title>Sarah Palin is a Parrot</title>
		<link>http://collingrady.wordpress.com/2008/10/14/sarah-palin-is-a-parrot/</link>
		<comments>http://collingrady.wordpress.com/2008/10/14/sarah-palin-is-a-parrot/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 07:08:15 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://collingrady.wordpress.com/?p=116</guid>
		<description><![CDATA[I really have to agree with John Cleese&#8230;

       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=116&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I really have to agree with John Cleese&#8230;</p>
<p><span style="text-align:center; display: block;"><a href="http://collingrady.wordpress.com/2008/10/14/sarah-palin-is-a-parrot/"><img src="http://img.youtube.com/vi/jMyNk8J1c8g/2.jpg" alt="" /></a></span></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=116&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2008/10/14/sarah-palin-is-a-parrot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>

		<media:content url="http://img.youtube.com/vi/jMyNk8J1c8g/2.jpg" medium="image" />
	</item>
		<item>
		<title>Deploying Projects With Capistrano</title>
		<link>http://collingrady.wordpress.com/2008/08/27/deploying-projects-with-capistrano/</link>
		<comments>http://collingrady.wordpress.com/2008/08/27/deploying-projects-with-capistrano/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 21:25:56 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://collingrady.wordpress.com/?p=99</guid>
		<description><![CDATA[Not too long ago at work, we added a few more servers to our production deployment to speed things up &#8211; however, this made deploying code changes a bit more complicated, as you had to remember to update several servers every time.  I decided to look into Capistrano, which I&#8217;d heard about before, and am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=99&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Not too long ago at work, we added a few more servers to our production deployment to speed things up &#8211; however, this made deploying code changes a bit more complicated, as you had to remember to update several servers every time.  I decided to look into <a href="http://www.capify.org/">Capistrano</a>, which I&#8217;d heard about before, and am glad to say it&#8217;s working very well, once I managed to figure it out.<br />
<span id="more-99"></span><br />
The only problem with <a href="http://www.capify.org/">Capistrano</a> is that it&#8217;s primarily used by rails devs, so all the examples I could ever find online were using it for rails, and mostly using the included &#8216;deploy&#8217; script, which is very rails-centric with migrations and such. Since we&#8217;re using python and javascript in our projects, those things didn&#8217;t apply well.</p>
<p>After some digging I&#8217;ve managed to get a nice little deploy script that will update many servers to the latest tag in svn, and can also roll back to the next-latest tag, in case something goes wrong <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre style="overflow:auto;"><span style="color:#0000ff;">role</span> :example, <span style="color:#008080;">"www.example.com"</span>, <span style="color:#008080;">"www2.example.com"</span>, <span style="color:#008080;">"www3.example.com"</span>

<span style="color:#0000ff;">set</span> :destination, <span style="color:#008080;">"/home/example/sources"</span>
<span style="color:#0000ff;">set</span> :repository, <span style="color:#008080;">"svn+ssh://svn.example.com/svn/oggllc"</span>

<span style="color:#0000ff;">def </span>ls<span style="color:#ff0000;">(</span><span style="color:#0000ff;">path</span><span style="color:#ff0000;">)</span>
    `svn ls #{path}`
<span style="color:#0000ff;">end</span>

<span style="color:#0000ff;">def </span>previous_tag<span style="color:#ff0000;">(</span>component<span style="color:#ff0000;">)</span>
    logger.debug <span style="color:#008080;">"querying previous #{component} tag..."</span>
    tags_dir <span style="color:#000000;">= </span><span style="color:#008080;">"#{repository}/tags/#{component}/"</span>
    ls<span style="color:#ff0000;">(</span>tags_dir<span style="color:#ff0000;">)</span>.<span style="color:#0000ff;">split</span><span style="color:#ff0000;">(</span><span style="color:#008080;">"\n"</span><span style="color:#ff0000;">)[</span><span style="color:#000000;">-</span><span style="color:#000000;">2</span><span style="color:#ff0000;">]</span>.<span style="color:#0000ff;">chomp</span><span style="color:#ff0000;">(</span><span style="color:#008080;">'/'</span><span style="color:#ff0000;">)</span>
<span style="color:#0000ff;">end</span>

<span style="color:#0000ff;">def </span>latest_tag<span style="color:#ff0000;">(</span>component<span style="color:#ff0000;">)</span>
    logger.debug <span style="color:#008080;">"querying latest #{component} tag..."</span>
    tags_dir <span style="color:#000000;">= </span><span style="color:#008080;">"#{repository}/tags/#{component}/"</span>
    ls<span style="color:#ff0000;">(</span>tags_dir<span style="color:#ff0000;">)</span>.<span style="color:#0000ff;">split</span><span style="color:#ff0000;">(</span><span style="color:#008080;">"\n"</span><span style="color:#ff0000;">)[</span><span style="color:#000000;">-</span><span style="color:#000000;">1</span><span style="color:#ff0000;">]</span>.<span style="color:#0000ff;">chomp</span><span style="color:#ff0000;">(</span><span style="color:#008080;">'/'</span><span style="color:#ff0000;">)</span>
<span style="color:#0000ff;">end</span>

<span style="color:#0000ff;">desc</span> <span style="color:#008080;">"Deploy latest example tag"</span>
<span style="color:#0000ff;">task</span> :deploy_example, :roles <span style="color:#000000;">=&gt; </span>:example <span style="color:#0000ff;">do</span>
    <span style="color:#0000ff;">set</span> :latest_rev, latest_tag<span style="color:#ff0000;">(</span><span style="color:#008080;">"example"</span><span style="color:#ff0000;">)</span>
    logger.debug <span style="color:#008080;">"latest tag: #{latest_rev}"</span>
    <span style="color:#0000ff;">run </span><span style="color:#008080;">"cd #{destination}/example ; svn switch #{repository}/tags/example/#{latest_rev}"</span>
    <span style="color:#0000ff;">run </span><span style="color:#008080;">"sudo apachectl restart"</span>
<span style="color:#0000ff;">end</span>

<span style="color:#0000ff;">desc</span> <span style="color:#008080;">"Rollback example to previous tag"</span>
<span style="color:#0000ff;">task</span> :rollback_example, :roles <span style="color:#000000;">=&gt; </span>:example <span style="color:#0000ff;">do</span>
    <span style="color:#0000ff;">set</span> :previous_rev, previous_tag<span style="color:#ff0000;">(</span><span style="color:#008080;">"example"</span><span style="color:#ff0000;">)</span>
    logger.debug <span style="color:#008080;">"previous tag: #{previous_rev}"</span>
    <span style="color:#0000ff;">run </span><span style="color:#008080;">"cd #{destination}/example ; svn switch #{repository}/tags/example/#{previous_rev}"</span>
    <span style="color:#0000ff;">run </span><span style="color:#008080;">"sudo apachectl restart"</span>
<span style="color:#0000ff;">end</span></pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collingrady.wordpress.com/99/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collingrady.wordpress.com/99/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/99/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=99&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2008/08/27/deploying-projects-with-capistrano/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>
	</item>
		<item>
		<title>Useful form tricks in Django</title>
		<link>http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/</link>
		<comments>http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 08:46:10 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://collingrady.wordpress.com/?p=87</guid>
		<description><![CDATA[Not much text to put in this post, I just want to show a few useful things you can do with a ModelForm in django, so I can quit retyping examples 

1) Replace a widget without losing all the default values passed to the form field from the model definition:
from django import forms
from mysite.polls.models import [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=87&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Not much text to put in this post, I just want to show a few useful things you can do with a <tt>ModelForm</tt> in django, so I can quit retyping examples <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-87"></span><br />
1) Replace a widget without losing all the default values passed to the form field from the model definition:</p>
<pre style="overflow:auto;"><span style="color:#0000FF;">from </span>django <span style="color:#0000FF;">import </span>forms
<span style="color:#0000FF;">from </span>mysite.polls.models <span style="color:#0000FF;">import </span>Poll

<span style="color:#0000FF;">class </span>PollForm<span style="color:#FF0000;">(</span>forms.ModelForm<span style="color:#FF0000;">)</span>:
    <span style="color:#0000FF;">def </span><span style="color:#0000FF;">__init__</span><span style="color:#FF0000;">(</span><span style="color:#004080;">self</span>, <span style="color:#000000;">*</span>args, <span style="color:#000000;">**</span>kwargs<span style="color:#FF0000;">)</span>:
        <span style="color:#0000FF;">super</span><span style="color:#FF0000;">(</span>PollForm, <span style="color:#004080;">self</span><span style="color:#FF0000;">)</span>.<span style="color:#0000FF;">__init__</span><span style="color:#FF0000;">(</span><span style="color:#000000;">*</span>args, <span style="color:#000000;">**</span>kwargs<span style="color:#FF0000;">)</span>
        <span style="color:#004080;">self</span>.fields<span style="color:#FF0000;">[</span><span style="color:#008080;">'question'</span><span style="color:#FF0000;">]</span>.widget <span style="color:#000000;">= </span>forms.Textarea<span style="color:#FF0000;">()</span>

    <span style="color:#0000FF;">class </span>Meta:
        model <span style="color:#000000;">= </span>Poll</pre>
<p>2) Along the same lines, limit the choices of a <tt>ForeignKey</tt> based on a user:</p>
<p><span style="font-size:.85em;">(assume we&#8217;ve added an <tt>owner</tt> field to the tutorial <tt>Poll</tt> model to indicate the <tt>User</tt> who created the <tt>Poll</tt>)</span></p>
<pre style="overflow:auto;"><span style="color:#0000FF;">from </span>datetime <span style="color:#0000FF;">import </span>datetime, timedelta
<span style="color:#0000FF;">from </span>django <span style="color:#0000FF;">import </span>forms
<span style="color:#0000FF;">from </span>mysite.polls.models <span style="color:#0000FF;">import </span>Poll, Choice

<span style="color:#0000FF;">class </span>ChoiceForm<span style="color:#FF0000;">(</span>forms.ModelForm<span style="color:#FF0000;">)</span>:
    <span style="color:#0000FF;">def </span><span style="color:#0000FF;">__init__</span><span style="color:#FF0000;">(</span><span style="color:#004080;">self</span>, user, <span style="color:#000000;">*</span>args, <span style="color:#000000;">**</span>kwargs<span style="color:#FF0000;">)</span>:
        <span style="color:#0000FF;">super</span><span style="color:#FF0000;">(</span>ChoiceForm, <span style="color:#004080;">self</span><span style="color:#FF0000;">)</span>.<span style="color:#0000FF;">__init__</span><span style="color:#FF0000;">(</span><span style="color:#000000;">*</span>args, <span style="color:#000000;">**</span>kwargs<span style="color:#FF0000;">)</span>
        <span style="color:#004080;">self</span>.fields<span style="color:#FF0000;">[</span><span style="color:#008080;">'poll'</span><span style="color:#FF0000;">]</span>.queryset <span style="color:#000000;">= </span>Poll.objects.<span style="color:#0000FF;">filter</span><span style="color:#FF0000;">(</span>owner<span style="color:#000000;">=</span>user<span style="color:#FF0000;">)</span>

    <span style="color:#0000FF;">class </span>Meta:
        model <span style="color:#000000;">= </span>Choice</pre>
<p>In this one, you would then call the form with <tt>request.user</tt> as the first argument:</p>
<pre style="overflow:auto;">form = ChoiceForm<span style="color:#FF0000;">(</span>request.user, request.POST<span style="color:#FF0000;">)</span></pre>
<p>or just</p>
<pre style="overflow:auto;">form = ChoiceForm<span style="color:#FF0000;">(</span>request.user<span style="color:#FF0000;">)</span></pre>
<p>3) Nothing yet, may expand this later.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collingrady.wordpress.com/87/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collingrady.wordpress.com/87/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=87&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>
	</item>
		<item>
		<title>Spam bots suck</title>
		<link>http://collingrady.wordpress.com/2008/03/23/spam-bots-suck/</link>
		<comments>http://collingrady.wordpress.com/2008/03/23/spam-bots-suck/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 07:09:15 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://collingrady.wordpress.com/2008/03/23/spam-bots-suck/</guid>
		<description><![CDATA[Today my blog was hacked, erasing the most recent post in favor of spam links. Thankfully, I was able to recover it from Google&#8217;s cache, but I&#8217;m tired of all the issues maintaining my own blog.
As a result, I&#8217;ve decided to move my blog here, so that I don&#8217;t have to deal with the hassle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=85&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today my blog was hacked, erasing the most recent post in favor of spam links. Thankfully, I was able to recover it from Google&#8217;s cache, but I&#8217;m tired of all the issues maintaining my own blog.</p>
<p>As a result, I&#8217;ve decided to move my blog here, so that I don&#8217;t have to deal with the hassle anymore.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collingrady.wordpress.com/85/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collingrady.wordpress.com/85/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=85&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2008/03/23/spam-bots-suck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>
	</item>
		<item>
		<title>Editing multiple objects in Django with forms</title>
		<link>http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/</link>
		<comments>http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 21:45:28 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://collingrady.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/</guid>
		<description><![CDATA[A common problem I see when helping people in the Django IRC channel is people who are trying to create or edit multiple objects, but for some reason try to make a single form object to deal with them &#8211; this makes things quite convoluted and much harder than they have to be. My goal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=84&subd=collingrady&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A common problem I see when helping people in the Django IRC channel is people who are trying to create or edit multiple objects, but for some reason try to make a single form object to deal with them &#8211; this makes things quite convoluted and much harder than they have to be. My goal here is to explain a much simpler method using multiple form objects.<br />
<span id="more-84"></span><br />
For this guide, I&#8217;m going to use the basic <tt>Poll</tt> and <tt>Choice</tt> models from the tutorial. I&#8217;m only showing the fields here, as that&#8217;s all that matters to the form, but they wouldn&#8217;t break anything, of course <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre style="overflow:auto;"><span style="color:#0000ff;">from </span>django.db <span style="color:#0000ff;">import </span>models

<span style="color:#0000ff;">class </span>Poll<span style="color:#ff0000;">(</span>models.Model<span style="color:#ff0000;">)</span>:
    question <span style="color:#000000;">= </span>models.CharField<span style="color:#ff0000;">(</span>max_length<span style="color:#000000;">=</span><span style="color:#000000;">200</span><span style="color:#ff0000;">)</span>
    pub_date <span style="color:#000000;">= </span>models.DateTimeField<span style="color:#ff0000;">()</span>

<span style="color:#0000ff;">class </span>Choice<span style="color:#ff0000;">(</span>models.Model<span style="color:#ff0000;">)</span>:
    poll <span style="color:#000000;">= </span>models.ForeignKey<span style="color:#ff0000;">(</span>Poll<span style="color:#ff0000;">)</span>
    choice <span style="color:#000000;">= </span>models.CharField<span style="color:#ff0000;">(</span>max_length<span style="color:#000000;">=</span><span style="color:#000000;">200</span><span style="color:#ff0000;">)</span>
    votes <span style="color:#000000;">= </span>models.IntegerField<span style="color:#ff0000;">(</span>default<span style="color:#000000;">=</span><span style="color:#000000;">0</span><span style="color:#ff0000;">)</span></pre>
<p>To start, we&#8217;ll need forms for each model.</p>
<pre style="overflow:auto;"><span style="color:#0000ff;">from </span>django <span style="color:#0000ff;">import </span>forms
<span style="color:#0000ff;">from </span>mysite.polls.models <span style="color:#0000ff;">import </span>Poll, Choice

<span style="color:#0000ff;">class </span>PollForm<span style="color:#ff0000;">(</span>forms.ModelForm<span style="color:#ff0000;">)</span>:
    <span style="color:#0000ff;">class </span>Meta:
        model <span style="color:#000000;">= </span>Poll

<span style="color:#0000ff;">class </span>ChoiceForm<span style="color:#ff0000;">(</span>forms.ModelForm<span style="color:#ff0000;">)</span>:
    <span style="color:#0000ff;">class </span>Meta:
        model <span style="color:#000000;">= </span>Choice
        exclude <span style="color:#000000;">= </span><span style="color:#ff0000;">(</span><span style="color:#008080;">'poll'</span>,<span style="color:#ff0000;">)</span></pre>
<p>By using <tt>ModelForm</tt>, all the grunt work of saving/updating is handled for us, so it saves a lot of work you&#8217;d have to code yourself when trying to use a single form.</p>
<p>Next up is the view, but here there&#8217;s one issue we have to avoid &#8211; if you simply make 3 <tt>ChoiceForm</tt> instances, their field names will conflict &#8211; since you&#8217;ll get 3 <tt>poll</tt> fields and 3 <tt>choice</tt> fields. To get around this, we need to use the prefix arg to the forms, as you&#8217;ll see in a moment.</p>
<pre style="overflow:auto;"><span style="color:#0000ff;">from </span>mysite.polls.models <span style="color:#0000ff;">import </span>Poll, Choice
<span style="color:#0000ff;">from </span>mysite.polls.forms <span style="color:#0000ff;">import </span>PollForm, ChoiceForm
<span style="color:#0000ff;">from </span>django.http <span style="color:#0000ff;">import </span>HttpResponseRedirect
<span style="color:#0000ff;">from </span>django.shortcuts <span style="color:#0000ff;">import </span>render_to_response

<span style="color:#0000ff;">def </span>add_poll<span style="color:#ff0000;">(</span>request<span style="color:#ff0000;">)</span>:
    <span style="color:#0000ff;">if </span>request.method <span style="color:#000000;">== </span><span style="color:#008080;">"POST"</span>:
        pform <span style="color:#000000;">= </span>PollForm<span style="color:#ff0000;">(</span>request.POST, instance<span style="color:#000000;">=</span>Poll<span style="color:#ff0000;">())</span>
        cforms <span style="color:#000000;">= </span><span style="color:#ff0000;">[</span>ChoiceForm<span style="color:#ff0000;">(</span>request.POST, prefix<span style="color:#000000;">=</span><span style="color:#0000ff;">str</span><span style="color:#ff0000;">(</span>x<span style="color:#ff0000;">)</span>, instance<span style="color:#000000;">=</span>Choice<span style="color:#ff0000;">()) </span><span style="color:#0000ff;">for </span>x <span style="color:#0000ff;">in </span><span style="color:#0000ff;">range</span><span style="color:#ff0000;">(</span><span style="color:#000000;">0</span>,<span style="color:#000000;">3</span><span style="color:#ff0000;">)]</span>
        <span style="color:#0000ff;">if </span>pform.is_valid<span style="color:#ff0000;">() </span><span style="color:#0000ff;">and </span>all<span style="color:#ff0000;">([</span>cf.is_valid<span style="color:#ff0000;">() </span><span style="color:#0000ff;">for </span>cf <span style="color:#0000ff;">in </span>cforms<span style="color:#ff0000;">])</span>:
            new_poll <span style="color:#000000;">= </span>pform.save<span style="color:#ff0000;">()</span>
            <span style="color:#0000ff;">for </span>cf <span style="color:#0000ff;">in </span>cforms:
                new_choice <span style="color:#000000;">= </span>cf.save<span style="color:#ff0000;">(</span>commit<span style="color:#000000;">=</span><span style="color:#0000ff;">False</span><span style="color:#ff0000;">)</span>
                new_choice.poll <span style="color:#000000;">= </span>new_poll
                new_choice.save<span style="color:#ff0000;">()</span>
            <span style="color:#0000ff;">return </span>HttpResponseRedirect<span style="color:#ff0000;">(</span><span style="color:#008080;">'/polls/add/'</span><span style="color:#ff0000;">)</span>
    <span style="color:#0000ff;">else</span>:
        pform <span style="color:#000000;">= </span>PollForm<span style="color:#ff0000;">(</span>instance<span style="color:#000000;">=</span>Poll<span style="color:#ff0000;">())</span>
        cforms <span style="color:#000000;">= </span><span style="color:#ff0000;">[</span>ChoiceForm<span style="color:#ff0000;">(</span>prefix<span style="color:#000000;">=</span><span style="color:#0000ff;">str</span><span style="color:#ff0000;">(</span>x<span style="color:#ff0000;">)</span>, instance<span style="color:#000000;">=</span>Choice<span style="color:#ff0000;">()) </span><span style="color:#0000ff;">for </span>x <span style="color:#0000ff;">in </span><span style="color:#0000ff;">range</span><span style="color:#ff0000;">(</span><span style="color:#000000;">0</span>,<span style="color:#000000;">3</span><span style="color:#ff0000;">)]</span>
    <span style="color:#0000ff;">return </span>render_to_response<span style="color:#ff0000;">(</span><span style="color:#008080;">'add_poll.html'</span>, <span style="color:#ff0000;">{</span><span style="color:#008080;">'poll_form'</span>: pform, <span style="color:#008080;">'choice_forms'</span>: cforms<span style="color:#ff0000;">})</span></pre>
<p>The basic structure is identical to when you&#8217;re adding a single object, with only a few differences. The first thing I&#8217;d like to point out is the <tt>prefix</tt> arg &#8211; by setting that, we get fields named <tt>0-choice</tt>, <tt>1-choice</tt>, and <tt>2-choice</tt> instead of just 3 fields named <tt>choice</tt>. This allows each <tt>ChoiceForm</tt> to pick out its own data from <tt>request.POST</tt>, so they don&#8217;t conflict.</p>
<p>When checking if the forms are valid, I use all<tt>(),</tt> which will only return <tt>True</tt> if every item in the list evaluates to <tt>True</tt> as well. For those who like <tt>map()</tt>, you can replace the inner list comprehension with <tt>map(lambda x: x.is_valid(), cforms)</tt>.</p>
<p>Note: It&#8217;s come to my attention that <tt>all()</tt> is new in 2.5, and I hadn&#8217;t been aware of that previously &#8211; a quick way to define your own is here:</p>
<pre style="overflow:auto;"><span style="color:#0000ff;">def </span>all<span style="color:#ff0000;">(</span><span style="color:#0000ff;">items</span><span style="color:#ff0000;">)</span>:
    <span style="color:#0000ff;">import </span>operator
    <span style="color:#0000ff;">return </span><span style="color:#0000ff;">reduce</span><span style="color:#ff0000;">(</span>operator.and_, <span style="color:#ff0000;">[</span><span style="color:#0000ff;">bool</span><span style="color:#ff0000;">(</span>item<span style="color:#ff0000;">) </span><span style="color:#0000ff;">for </span>item <span style="color:#0000ff;">in </span><span style="color:#0000ff;">items</span><span style="color:#ff0000;">])</span></pre>
<p>There may be other methods, but this should work fine <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Finally, when saving the forms, we use <tt>commit=False</tt> on the <tt>ChoiceForm</tt> instances, so that we can set the <tt>poll</tt> ForeignKey properly before saving. The <tt>commit</tt> arg tells the form to create the new <tt>Choice</tt> instance and return it, but not save it to the database.</p>
<p>And with that, you now have a view which can add a <tt>Poll</tt> and 3 <tt>Choice</tt> objects for it at once.</p>
<p>Making a view to edit the same objects is much similar, you just need to query the <tt>Poll</tt> and <tt>Choice</tt> objects and use them instead of the empty instances. You can also use the <tt>Choice</tt> IDs as the prefixes for the <tt>ChoiceForm</tt> instances. The great thing with using <tt>ModelForm</tt> is that once you change the instance arg from a new object to an existing one, it becomes an edit form instead of an add form, so you don&#8217;t have to change any internals to go from adding to editing.</p>
<p><em>Updated 11/3/08 for django 1.0</em></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/collingrady.wordpress.com/84/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/collingrady.wordpress.com/84/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/collingrady.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/collingrady.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/collingrady.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/collingrady.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/collingrady.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/collingrady.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/collingrady.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/collingrady.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/collingrady.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/collingrady.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=collingrady.wordpress.com&blog=240174&post=84&subd=collingrady&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6c3c902d82680d3c051115ebe2b422c1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">magus424</media:title>
		</media:content>
	</item>
	</channel>
</rss>