<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dealing it</title>
	<atom:link href="http://dealingit.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dealingit.wordpress.com</link>
	<description>Dealit, Django, Startup, UI</description>
	<lastBuildDate>Mon, 08 Aug 2011 14:17:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='dealingit.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Dealing it</title>
		<link>http://dealingit.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dealingit.wordpress.com/osd.xml" title="Dealing it" />
	<atom:link rel='hub' href='http://dealingit.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Django tip: Variable aliasing using {% with %} tags</title>
		<link>http://dealingit.wordpress.com/2009/11/04/django-tip-variable-aliasing-using-with-tags/</link>
		<comments>http://dealingit.wordpress.com/2009/11/04/django-tip-variable-aliasing-using-with-tags/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 20:38:36 +0000</pubDate>
		<dc:creator>dealingit</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://dealingit.wordpress.com/?p=45</guid>
		<description><![CDATA[Say you have an include that loops over a list like this&#8230; # render_customers.html {% for c in customers %} {{ c.first_name }} {% endfor %} That is, it loops over a list called customers and only a list called &#8230; <a href="http://dealingit.wordpress.com/2009/11/04/django-tip-variable-aliasing-using-with-tags/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dealingit.wordpress.com&amp;blog=10050627&amp;post=45&amp;subd=dealingit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Say you have an include that loops over a list like this&#8230;</p>
<pre>
  # render_customers.html

  {% for c in customers %}
    {{ c.first_name }}
  {% endfor %}
</pre>
<p>That is, it loops over a list called customers and <strong>only</strong> a list called customers. Now say you want to use it multiple times in your template with different lists &#8211; e.g. one called new_customers and another called old_customers &#8211; then you&#8217;d do it like this&#8230;</p>
<pre>
{% with <strong>new_customers</strong> as customers %}
  {% include "render_customers.html" %}
{% endwith %}

{% with <strong>old_customers</strong> as customers %}
  {% include "render_customers.html" %}
{% endwith %}
</pre>
<p>A simple trick but one that I don&#8217;t believe is clearly laid out in the otherwise fantastic Django documentation. Here&#8217;s what the blurb for the {% with %} tag says in the docs: &#8220;Caches a complex variable under a simpler name. This is useful when accessing an &#8220;expensive&#8221; method (e.g., one that hits the database) multiple times.&#8221;</p>
<p>I didn&#8217;t understand the purpose of the tag until I came across a one-line mention of it in the <a href="http://docs.djangoproject.com/en/dev/topics/forms/#reusable-form-templates">reusable form templates</a> section.</p>
<p>Hope it helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dealingit.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dealingit.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dealingit.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dealingit.wordpress.com&amp;blog=10050627&amp;post=45&amp;subd=dealingit&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://dealingit.wordpress.com/2009/11/04/django-tip-variable-aliasing-using-with-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6b8010858c08c6c12b97826f8d623f08?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dealingit</media:title>
		</media:content>
	</item>
		<item>
		<title>Django tip: Showing &lt;optgroup&gt;s in a ModelForm</title>
		<link>http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/</link>
		<comments>http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 17:38:18 +0000</pubDate>
		<dc:creator>dealingit</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://dealingit.wordpress.com/?p=9</guid>
		<description><![CDATA[For my first Django tip on this brand new blog, I will provide some insight into my most recent head-scratcher: How do you get a ModelForm to display something like this&#8230; &#8230;when you have your models laid out like this: &#8230; <a href="http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dealingit.wordpress.com&amp;blog=10050627&amp;post=9&amp;subd=dealingit&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For my first Django tip on this brand new blog, I will provide some insight into my most recent head-scratcher: How do you get a ModelForm to display something like this&#8230;</p>
<p style="text-align:center;"><img class="size-full wp-image-17 aligncenter" title="Choices" src="http://dealingit.files.wordpress.com/2009/10/screen-shot-2009-10-26-at-12-08-27-pm.png?w=203&#038;h=176" alt="" width="203" height="176" /></p>
<p>&#8230;when you have your models laid out like this:</p>
<pre>class Category(models.Model):
    name = models.CharField(max_length=50)  # Auto, Beauty, etc.

class SubCategory(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField(max_length=50)  # Parts, Fragrances, etc.

class Merchant(models.Model):
    sub_categories = models.ManyToManyField(SubCategory, verbose_name='Areas your business deals in')</pre>
<p>On <a href="http://www.dealit.com">Dealit</a>, the SubCategory model is used by many other models in a ManyToMany relationship, for example, when a merchant signs up (as I&#8217;m showing in the Merchant model above). But building out a ModelForm for Merchant will only display SubCategories without the related Category in an &lt;optgroup&gt;.</p>
<p>In order to fix that, we do this in our form&#8230;</p>
<pre>class MerchantForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(MerchantForm, self).__init__(*args, **kwargs)
        self.fields['sub_categories'].choices = <span style="color:#ff0000;">categories_as_choices()</span>

def <span style="color:#ff0000;">categories_as_choices()</span>:
    categories = []
    for category in Category.objects.all():
        new_category = []
        sub_categories = []
        for sub_category in category.subcategory_set.all():
            sub_categories.append([sub_category.id, sub_category.name])

        new_category = [category.name, sub_categories]
        categories.append(new_category)

    return categories</pre>
<p>Here, we create a lot of nested lists with categories_as_choices() and tell Django to override the default list in the form. This is how Django wants it if you want it displaying &lt;optgroup&gt;s. Here&#8217;s an example from the <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#choices">documentation</a>:</p>
<pre>MEDIA_CHOICES = (
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
)</pre>
<p>This is exactly what we end up creating with categories_as_choices(), except we create lists instead of tuples. Both are acceptable.</p>
<p>Hope that helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dealingit.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dealingit.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dealingit.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dealingit.wordpress.com&amp;blog=10050627&amp;post=9&amp;subd=dealingit&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6b8010858c08c6c12b97826f8d623f08?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dealingit</media:title>
		</media:content>

		<media:content url="http://dealingit.files.wordpress.com/2009/10/screen-shot-2009-10-26-at-12-08-27-pm.png" medium="image">
			<media:title type="html">Choices</media:title>
		</media:content>
	</item>
	</channel>
</rss>
