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

<channel>
	<title>tech.nickserra.com</title>
	<atom:link href="http://tech.nickserra.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.nickserra.com</link>
	<description></description>
	<lastBuildDate>Thu, 05 Apr 2012 17:56:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>python-cloudcracker: a class to interface with the CloudCracker API</title>
		<link>http://tech.nickserra.com/2012/04/05/python-cloudcracker-a-class-to-interface-with-the-cloudcracker-api/</link>
		<comments>http://tech.nickserra.com/2012/04/05/python-cloudcracker-a-class-to-interface-with-the-cloudcracker-api/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 17:56:19 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=19</guid>
		<description><![CDATA[So I released a quick python class to interface with the CloudCracker API. CloudCracker is a new online password cracking service. Upload a pcap file, pay via BitCoin or Stripe, and they will crack the password for you. Pretty cool. &#8230; <a href="http://tech.nickserra.com/2012/04/05/python-cloudcracker-a-class-to-interface-with-the-cloudcracker-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I released a quick python class to interface with the CloudCracker API. CloudCracker is a new online password cracking service. Upload a pcap file, pay via BitCoin or Stripe, and they will crack the password for you. Pretty cool. Check out CloudCracker at <a href="http://cloudcracker.com">cloudcracker.com</a> and check out <a href="https://github.com/nicholasserra/python-cloudcracker">python-cloudcracker</a> on GitHub.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2012/04/05/python-cloudcracker-a-class-to-interface-with-the-cloudcracker-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ultramemcache: python-ultramemcached and django-ultramemached-cache</title>
		<link>http://tech.nickserra.com/2012/02/11/ultramemcache-python-ultramemcached-and-django-ultramemached-cache/</link>
		<comments>http://tech.nickserra.com/2012/02/11/ultramemcache-python-ultramemcached-and-django-ultramemached-cache/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 20:17:27 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[cache backend]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ultramemcache]]></category>
		<category><![CDATA[ultramemcached]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=15</guid>
		<description><![CDATA[A few months ago I came across a package on GitHub called Ultramemcache, which was described as &#8220;Ultra fast memcache client written in highly optimized C++ with Python bindings.&#8221; Using Ulramemcache, I modified the common python-memcached library to use Ultramemcache, &#8230; <a href="http://tech.nickserra.com/2012/02/11/ultramemcache-python-ultramemcached-and-django-ultramemached-cache/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few months ago I came across a package on GitHub called <a href="https://github.com/esnme/ultramemcache">Ultramemcache</a>, which was described as &#8220;Ultra fast memcache client written in highly optimized C++ with Python bindings.&#8221; </p>
<p>Using Ulramemcache, I modified the common python-memcached library to use Ultramemcache, making sure it could be used as a drop in replacement. Check out <a href="https://github.com/nicholasserra/python-ultramemcached">python-ultramemcached</a> on my <a href="https://github.com/nicholasserra/">GitHub</a> page.</p>
<p>I also put together a django cache backend using Ultramemcache. This currently does not require my python-ultramemcached library. Check out <a href="https://github.com/nicholasserra/django-ultramemcached-cache">django-ultramemcached-cache</a> on my <a href="https://github.com/nicholasserra/">GitHub</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2012/02/11/ultramemcache-python-ultramemcached-and-django-ultramemached-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A python class for the Exact Target XML api</title>
		<link>http://tech.nickserra.com/2012/02/11/a-python-class-for-the-exact-target-email-api/</link>
		<comments>http://tech.nickserra.com/2012/02/11/a-python-class-for-the-exact-target-email-api/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 20:04:05 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[exact target]]></category>
		<category><![CDATA[exacttarget]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[subscriber]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=12</guid>
		<description><![CDATA[After dealing with Exact Target&#8216;s XML api for their email list management system, I decided to package up a few functions into a class. Check out python-exacttarget on my GitHub page.]]></description>
			<content:encoded><![CDATA[<p>After dealing with <a href="http://www.exacttarget.com/">Exact Target</a>&#8216;s XML api for their email list management system, I decided to package up a few functions into a class. Check out <a href="https://github.com/nicholasserra/python-exacttarget">python-exacttarget</a> on my <a href="https://github.com/nicholasserra/">GitHub</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2012/02/11/a-python-class-for-the-exact-target-email-api/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django runserver and gevent; NotImplementedError: gevent is only usable from a single thread</title>
		<link>http://tech.nickserra.com/2011/11/16/django-runserver-and-gevent-notimplementederror-gevent-is-only-usable-from-a-single-thread/</link>
		<comments>http://tech.nickserra.com/2011/11/16/django-runserver-and-gevent-notimplementederror-gevent-is-only-usable-from-a-single-thread/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 21:20:51 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[gevent]]></category>
		<category><![CDATA[manage.py]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[requests]]></category>
		<category><![CDATA[runserver]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=10</guid>
		<description><![CDATA[Hey. So I ran into a problem when using Django runserver and Requests for Python. Specifically, when running runserver, I came across the error: NotImplementedError: gevent is only usable from a single thread To fix this, you need to run &#8230; <a href="http://tech.nickserra.com/2011/11/16/django-runserver-and-gevent-notimplementederror-gevent-is-only-usable-from-a-single-thread/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hey. So I ran into a problem when using Django runserver and <a href="https://github.com/kennethreitz/requests">Requests</a> for Python. Specifically, when running runserver, I came across the error:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">NotImplementedError</span>: gevent <span class="kw1">is</span> only usable <span class="kw1">from</span> a single <span class="kw3">thread</span></div>
</li>
</ol>
</div>
<p>To fix this, you need to run the gevent monkey patch in your projects manage.py.</p>
<p>Add the following lines in the __main__ of your manage.py</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">from</span> gevent <span class="kw1">import</span> monkey</div>
</li>
<li class="li1">
<div class="de1">monkey.<span class="me1">patch_all</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Your main will look as follows:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> __name__ == <span class="st0">&quot;__main__&quot;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">import</span> gevent</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">from</span> gevent <span class="kw1">import</span> monkey</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; monkey.<span class="me1">patch_all</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; execute_manager<span class="br0">&#40;</span>settings<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2011/11/16/django-runserver-and-gevent-notimplementederror-gevent-is-only-usable-from-a-single-thread/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>phpFlickr 3.1 cache unserialize error</title>
		<link>http://tech.nickserra.com/2011/06/09/phpflickr-3-1-cache-unserialize-error/</link>
		<comments>http://tech.nickserra.com/2011/06/09/phpflickr-3-1-cache-unserialize-error/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:19:23 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[3.1]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[base64_encode]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[flickr api]]></category>
		<category><![CDATA[phpflickr]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unserialize]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=9</guid>
		<description><![CDATA[I came across a problem today with phpFlickr 3.1 and the way it handles its database caching. I believe it has to do with commas in the serialized data that is being inserted into the cache table. After grabbing the &#8230; <a href="http://tech.nickserra.com/2011/06/09/phpflickr-3-1-cache-unserialize-error/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I came across a problem today with phpFlickr 3.1 and the way it handles its database caching. I believe it has to do with commas in the serialized data that is being inserted into the cache table. After grabbing the cache result php gave me this error in the phpFlickr library:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">Notice<span class="sy0">:</span> <span class="kw3">unserialize</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">:</span> Error at offset <span class="sy0">&#8230;</span> phpFlickr<span class="sy0">.</span>php</div>
</li>
</ol>
</div>
<p>To fix this error you need to base64 encode and decode the data going into the database.</p>
<p>in phpFlickr modify line 144 to decode the response:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">return</span> <span class="kw3">base64_decode</span><span class="br0">&#40;</span><span class="re1">$result</span><span class="br0">&#91;</span><span class="st0">&#39;response&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>line 177 on cache row update:</p>
<div class="geshi php">
<ol start="177">
<li class="li1">
<div class="de1"><span class="re1">$sql</span> <span class="sy0">=</span> <span class="st0">&quot;UPDATE &quot;</span> <span class="sy0">.</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">cache_table</span> <span class="sy0">.</span> <span class="st0">&quot; SET response = &#39;&quot;</span> <span class="sy0">.</span> <span class="kw3">str_replace</span><span class="br0">&#40;</span><span class="st0">&quot;&#39;&quot;</span><span class="sy0">,</span> <span class="st0">&quot;&#39;&#39;&quot;</span><span class="sy0">,</span> <span class="kw3">base64_encode</span><span class="br0">&#40;</span><span class="re1">$response</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;&#39;, expiration = &#39;&quot;</span> <span class="sy0">.</span> <span class="kw3">strftime</span><span class="br0">&#40;</span><span class="st0">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;&#39; WHERE request = &#39;&quot;</span> <span class="sy0">.</span> <span class="re1">$reqhash</span> <span class="sy0">.</span> <span class="st0">&quot;&#39;&quot;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>line 180 on cache row insert:</p>
<div class="geshi php">
<ol start="180">
<li class="li1">
<div class="de1"><span class="re1">$sql</span> <span class="sy0">=</span> <span class="st0">&quot;INSERT INTO &quot;</span> <span class="sy0">.</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">cache_table</span> <span class="sy0">.</span> <span class="st0">&quot; (request, response, expiration) VALUES (&#39;$reqhash&#39;, &#39;&quot;</span> <span class="sy0">.</span> <span class="kw3">str_replace</span><span class="br0">&#40;</span><span class="st0">&quot;&#39;&quot;</span><span class="sy0">,</span> <span class="st0">&quot;&#39;&#39;&quot;</span><span class="sy0">,</span> <span class="kw3">base64_encode</span><span class="br0">&#40;</span><span class="re1">$response</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;&#39;, &#39;&quot;</span> <span class="sy0">.</span> <span class="kw3">strftime</span><span class="br0">&#40;</span><span class="st0">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;&#39;)&quot;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Thanks to David Walsh for his php serialize tips:<br />
<a href="http://davidwalsh.name/php-serialize-unserialize-issues">http://davidwalsh.name/php-serialize-unserialize-issues</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2011/06/09/phpflickr-3-1-cache-unserialize-error/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django AuthenticationForm and non field errors</title>
		<link>http://tech.nickserra.com/2011/06/07/django-authenticationform-and-non-field-errors/</link>
		<comments>http://tech.nickserra.com/2011/06/07/django-authenticationform-and-non-field-errors/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 16:21:03 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[authentication form]]></category>
		<category><![CDATA[authenticationform]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[field errors]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[non field errors]]></category>
		<category><![CDATA[non_field_errors]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=8</guid>
		<description><![CDATA[Here&#8217;s a quick tip for the built in Django AuthenticationForm in django.contrib.auth.forms: use the non_field_errors method when trying to display the incorrect user/pass error. The error is raised in the general clean method and is not bound to any field, &#8230; <a href="http://tech.nickserra.com/2011/06/07/django-authenticationform-and-non-field-errors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip for the built in Django AuthenticationForm in django.contrib.auth.forms: use the non_field_errors method when trying to display the incorrect user/pass error. The error is raised in the general clean method and is not bound to any field, so using form.errors doesn&#8217;t give you the error message itself. </p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>div <span class="kw1">class</span>=<span class="st0">&quot;error&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span><span class="br0">&#123;</span> form.<span class="me1">non_field_errors</span> <span class="br0">&#125;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>/div<span class="sy0">&gt;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2011/06/07/django-authenticationform-and-non-field-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django forms: Adding a blank option to a required choice field</title>
		<link>http://tech.nickserra.com/2011/06/03/django-forms-adding-a-blank-option-to-a-required-choice-field/</link>
		<comments>http://tech.nickserra.com/2011/06/03/django-forms-adding-a-blank-option-to-a-required-choice-field/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 19:13:50 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[blank value]]></category>
		<category><![CDATA[choicefield]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[required]]></category>
		<category><![CDATA[required field]]></category>

		<guid isPermaLink="false">http://tech.nickserra.com/?p=6</guid>
		<description><![CDATA[Recently I came across a problem where I had a django form choicefield that needed to be required, but also needed a blank choice in case the user didn&#8217;t see the field and incorrectly submitted the first value. I saw &#8230; <a href="http://tech.nickserra.com/2011/06/03/django-forms-adding-a-blank-option-to-a-required-choice-field/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently I came across a problem where I had a django form choicefield that needed to be required, but also needed a blank choice in case the user didn&#8217;t see the field and incorrectly submitted the first value. I saw a stack overflow post recommending to make a clean method to check that the first choice isn&#8217;t selected. That post is here: </p>
<p><a href="http://stackoverflow.com/questions/5289491/blank-option-in-required-choicefield<br />
">http://stackoverflow.com/questions/5289491/blank-option-in-required-choicefield<br />
</a></p>
<p>&#8230; but a better way to do this is to simply leave the value of the blank choice as an empty string, which will invalidate the form without any extra clean method, like below:</p>
<div class="geshi python">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">CHOICES_WITH_BLANK = <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span><span class="st0">&#39;&#39;</span>, <span class="st0">&#39;&#8212;&#8212;&#8211;&#39;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span><span class="st0">&#39;1&#39;</span>, <span class="st0">&#39;choice one&#39;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span><span class="st0">&#39;2&#39;</span>, <span class="st0">&#39;choice two&#39;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">class</span> ChoiceForm<span class="br0">&#40;</span>forms.<span class="me1">Form</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;choice_field = forms.<span class="me1">ChoiceField</span><span class="br0">&#40;</span>choices=CHOICES_WITH_BLANK<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2011/06/03/django-forms-adding-a-blank-option-to-a-required-choice-field/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django forms: passing variables to a modelchoicefield</title>
		<link>http://tech.nickserra.com/2011/05/16/django-forms-passing-variables-to-a-modelchoicefield/</link>
		<comments>http://tech.nickserra.com/2011/05/16/django-forms-passing-variables-to-a-modelchoicefield/#comments</comments>
		<pubDate>Mon, 16 May 2011 03:00:58 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[django forms]]></category>
		<category><![CDATA[form init]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[init]]></category>
		<category><![CDATA[modelchoicefield]]></category>
		<category><![CDATA[queryset]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://www.tech.nickserra.com/?p=4</guid>
		<description><![CDATA[So I came across the problem of needing to filter my Django ModelChoiceField queryset by a variable in my view. Specifically I needed to pass the current selected state to the ModelChoiceField queryset. The best way I found to do &#8230; <a href="http://tech.nickserra.com/2011/05/16/django-forms-passing-variables-to-a-modelchoicefield/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I came across the problem of needing to filter my Django ModelChoiceField queryset by a variable in my view. Specifically I needed to pass the current selected state to the ModelChoiceField queryset. The best way I found to do this is to initialize the form with my state var in the initial:</p>
<div class="geshi python">
<ol>
<li class="li1">
<div class="de1">state = <span class="st0">&#39;OH&#39;</span></div>
</li>
<li class="li1">
<div class="de1">form = LocationForm<span class="br0">&#40;</span>initial=<span class="br0">&#123;</span><span class="st0">&#39;state&#39;</span>:state<span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>After that, instead of creating the field the traditional way, you can append the field to the self.fields dictionary within the init, enabling you to pass the initial state variable into the queryset of the field. Here is my form:</p>
<div class="geshi python">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> LocationForm<span class="br0">&#40;</span>forms.<span class="me1">Form</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, <span class="sy0">*</span>args, <span class="sy0">**</span>kwargs<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">super</span><span class="br0">&#40;</span>LocationForm, <span class="kw2">self</span><span class="br0">&#41;</span>.<span class="kw4">__init__</span><span class="br0">&#40;</span><span class="sy0">*</span>args, <span class="sy0">**</span>kwargs<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">fields</span>.<span class="me1">insert</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">fields</span><span class="br0">&#41;</span><span class="nu0">-1</span>, <span class="st0">&#39;location&#39;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;forms.<span class="me1">ModelChoiceField</span><span class="br0">&#40;</span>queryset=Location.<span class="me1">objects</span>.<span class="kw2">filter</span><span class="br0">&#40;</span>state=<span class="kw2">self</span>.<span class="me1">initial</span><span class="br0">&#91;</span><span class="st0">&#39;state&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; first_name = forms.<span class="me1">CharField</span><span class="br0">&#40;</span><span class="nu0">50</span>, label=<span class="st0">&quot;First Name&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; last_name = forms.<span class="me1">CharField</span><span class="br0">&#40;</span><span class="nu0">50</span>, label=<span class="st0">&quot;Last Name&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; address = forms.<span class="me1">CharField</span><span class="br0">&#40;</span><span class="nu0">60</span>, label=<span class="st0">&quot;Street Address&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; city = forms.<span class="me1">CharField</span><span class="br0">&#40;</span><span class="nu0">40</span>, label=<span class="st0">&quot;City&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; state = USStateField<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">zip</span> = USZipCodeField<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Self.fields is a Django SortedDict, and it&#8217;s insert method takes the index to insert to, the key, and the value.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.nickserra.com/2011/05/16/django-forms-passing-variables-to-a-modelchoicefield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

