<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Using dynamic choices with Django newforms and custom widgets</title>
	<atom:link href="http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/</link>
	<description>Por Roberto Zoia</description>
	<lastBuildDate>Thu, 12 Aug 2010 21:48:40 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Paul</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-11786</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Fri, 10 Oct 2008 08:00:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-11786</guid>
		<description>&lt;p&gt;Awesome post.&lt;/p&gt;

&lt;p&gt;You saved me a ton of time trying to figure this out myself.&lt;/p&gt;

&lt;p&gt;Thanks again!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Awesome post.</p>

<p>You saved me a ton of time trying to figure this out myself.</p>

<p>Thanks again!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-8226</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Mon, 03 Dec 2007 19:35:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-8226</guid>
		<description>&lt;p&gt;Nice tips, was struggling with that. Still struggling thou, as I want to combine what you and Tom write, that is changing the widget, keeping the choices but only a subset.&lt;/p&gt;

&lt;p&gt;Problem I&#039;m faced with is a order-form. I seperate the Customer type, Residential and Corporate. In my Order model, I have a ManyToManyField for Products. Then I create a ModelForm (as form_for_model now is deprecated), and based on what Customer type is chosen (runtime), I only want to display a subset of the Products.&lt;/p&gt;

&lt;p&gt;So, atm I&#039;m still forced to query the database twice... :(&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Nice tips, was struggling with that. Still struggling thou, as I want to combine what you and Tom write, that is changing the widget, keeping the choices but only a subset.</p>

<p>Problem I&#8217;m faced with is a order-form. I seperate the Customer type, Residential and Corporate. In my Order model, I have a ManyToManyField for Products. Then I create a ModelForm (as form_for_model now is deprecated), and based on what Customer type is chosen (runtime), I only want to display a subset of the Products.</p>

<p>So, atm I&#8217;m still forced to query the database twice&#8230; <img src='http://www.zoia.org/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>]]></content:encoded>
	</item>
	<item>
		<title>By: Roberto</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-7337</link>
		<dc:creator>Roberto</dc:creator>
		<pubDate>Wed, 31 Oct 2007 15:36:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-7337</guid>
		<description>&lt;p&gt;Stan:
    Thanks for the comment.  I must that after reading my code again, I prefer your suggestion:  the formfield_callback is cleaner code.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Stan:
    Thanks for the comment.  I must that after reading my code again, I prefer your suggestion:  the formfield_callback is cleaner code.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Stan</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-7327</link>
		<dc:creator>Stan</dc:creator>
		<pubDate>Wed, 31 Oct 2007 10:18:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-7327</guid>
		<description>&lt;p&gt;You probably know this already, but another way of acheiving this, is using the &quot;formfield_callback&quot; argument to the &quot;form_for_model&quot; and &quot;form_for_instance&quot; methods. An example:&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;
def article_form_callback(f, **kwds):
&#160;&#160;&#160;&#160;if f.name == &#039;tags&#039;:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return f.formfield(widget=forms.CheckboxSelectMultiple, **kwds)
&#160;&#160;&#160;&#160;elif f.name == &#039;author&#039;:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return f.formfield(widget=forms.RadioSelect, **kwds)
&#160;&#160;&#160;&#160;else:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return f.formfield( **kwds)
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then you&#039;d do:&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;
ArticleForm = forms.form_for_model(Article, formfield_callback=article_form_callback)
...
ArticleForm = forms.form_for_instance(article_instance, formfield_callback=article_form_callback)
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I rather prefer this version -- it seems less &quot;hackish&quot; :), but I guess it&#039;s a matter of taste.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>You probably know this already, but another way of acheiving this, is using the &#8220;formfield_callback&#8221; argument to the &#8220;form_for_model&#8221; and &#8220;form_for_instance&#8221; methods. An example:</p>

<p>
<code>
def article_form_callback(f, **kwds):
&nbsp;&nbsp;&nbsp;&nbsp;if f.name == 'tags':
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f.formfield(widget=forms.CheckboxSelectMultiple, **kwds)
&nbsp;&nbsp;&nbsp;&nbsp;elif f.name == 'author':
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f.formfield(widget=forms.RadioSelect, **kwds)
&nbsp;&nbsp;&nbsp;&nbsp;else:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f.formfield( **kwds)
</code></p>

<p>Then you&#8217;d do:</p>

<p>
<code>
ArticleForm = forms.form_for_model(Article, formfield_callback=article_form_callback)
...
ArticleForm = forms.form_for_instance(article_instance, formfield_callback=article_form_callback)
</code></p>

<p>I rather prefer this version &#8212; it seems less &#8220;hackish&#8221; <img src='http://www.zoia.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but I guess it&#8217;s a matter of taste.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2007-08-31 &#171; PaxoBlog</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-6379</link>
		<dc:creator>links for 2007-08-31 &#171; PaxoBlog</dc:creator>
		<pubDate>Fri, 31 Aug 2007 23:24:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-6379</guid>
		<description>&lt;p&gt;[...] Using dynamic choices with Django newforms and custom widgets &#124; zoia.org I also include a working example of a sample Django blogging application I wrote for testing: pretty basic, but it and ilustrates, among other things, the use of custom widgets with dynamic choices. (tags: ajax django dynamic newforms templates tutorial widgets forms sample blog application) [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[...] Using dynamic choices with Django newforms and custom widgets | zoia.org I also include a working example of a sample Django blogging application I wrote for testing: pretty basic, but it and ilustrates, among other things, the use of custom widgets with dynamic choices. (tags: ajax django dynamic newforms templates tutorial widgets forms sample blog application) [...]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Roberto</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-6158</link>
		<dc:creator>Roberto</dc:creator>
		<pubDate>Mon, 20 Aug 2007 21:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-6158</guid>
		<description>&lt;p&gt;Tom:
   I know I can populate a selectbox using a queryset.  Let me try to explain the problem I wanted to solve.
   1. I create a form for my model using form_for_model o form_for_instance.  By default, foreign keys are represented as select boxes.  The choices are read dynamically from a table, that works.
   2. But I don&#039;t want a select box... I want checkboxes for my foreign key.  So, I
        a) retrieve the choices from the select box widget
        b) create a new widget (a CheckboxSelectMultiple) and feed it  the choices from a).&lt;br /&gt;
        c) replace the select box widget in the form with the new widget form b)
    Doing this (instead of using a query to obtain the choices for the CheckboxSelectMultiple widget) I don&#039;t need to query the database another time.  The first time was when Django created the SelectBox in the form_for_model call.
   I hope this makes it clearer.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Tom:
   I know I can populate a selectbox using a queryset.  Let me try to explain the problem I wanted to solve.
   1. I create a form for my model using form_for_model o form_for_instance.  By default, foreign keys are represented as select boxes.  The choices are read dynamically from a table, that works.
   2. But I don&#8217;t want a select box&#8230; I want checkboxes for my foreign key.  So, I
        a) retrieve the choices from the select box widget
        b) create a new widget (a CheckboxSelectMultiple) and feed it  the choices from a).<br />
        c) replace the select box widget in the form with the new widget form b)
    Doing this (instead of using a query to obtain the choices for the CheckboxSelectMultiple widget) I don&#8217;t need to query the database another time.  The first time was when Django created the SelectBox in the form_for_model call.
   I hope this makes it clearer.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tom L</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-6131</link>
		<dc:creator>Tom L</dc:creator>
		<pubDate>Sun, 19 Aug 2007 08:04:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-6131</guid>
		<description>&lt;p&gt;Thanks for that, it was very helpful.  Perhaps I didn&#039;t completely understand the tutorial but I wasn&#039;t able to find out how to dynamically populate a select box by querying a table. Maybe you want a subset of the table. I looked around and someone posted this on the google groups:&lt;/p&gt;

&lt;p&gt;choices=forms.models.QuerySetIterator(User.objects.all(), &quot;&quot;, False)&lt;/p&gt;

&lt;p&gt;of course, you can also to this:
choices=forms.models.QuerySetIterator(User.objects.filter(id=1), &quot;&quot;, False) or whatever query you need.&lt;/p&gt;

&lt;p&gt;Now you can populate a form element based on a query.  I post it here because it seems relevant to your write up.&lt;/p&gt;

&lt;p&gt;Here&#039;s the link to the group:
http://groups.google.com/group/django-users/browse_thread/thread/79e4c8fda31388e3/66b7a638054d68bc?lnk=gst&amp;q=dynamic+form&amp;rnum=6#66b7a638054d68bc&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for that, it was very helpful.  Perhaps I didn&#8217;t completely understand the tutorial but I wasn&#8217;t able to find out how to dynamically populate a select box by querying a table. Maybe you want a subset of the table. I looked around and someone posted this on the google groups:</p>

<p>choices=forms.models.QuerySetIterator(User.objects.all(), &#8220;&#8221;, False)</p>

<p>of course, you can also to this:
choices=forms.models.QuerySetIterator(User.objects.filter(id=1), &#8220;&#8221;, False) or whatever query you need.</p>

<p>Now you can populate a form element based on a query.  I post it here because it seems relevant to your write up.</p>

<p>Here&#8217;s the link to the group:
<a href="http://groups.google.com/group/django-users/browse_thread/thread/79e4c8fda31388e3/66b7a638054d68bc?lnk=gst&amp;q=dynamic+form&amp;rnum=6#66b7a638054d68bc" rel="nofollow">http://groups.google.com/group/django-users/browse_thread/thread/79e4c8fda31388e3/66b7a638054d68bc?lnk=gst&amp;q=dynamic+form&amp;rnum=6#66b7a638054d68bc</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: I LOVE open source &#171; Brian&#8217;s Blog</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-4789</link>
		<dc:creator>I LOVE open source &#171; Brian&#8217;s Blog</dc:creator>
		<pubDate>Wed, 30 May 2007 14:41:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-4789</guid>
		<description>&lt;p&gt;[...] May 30, 2007 at 8:41 am &#183; Filed under Django   I&#8217;ve been exploring Django&#8217;s newforms API.  Which is very sweet.  I feel that it is much improved over the old manipulator system.  I really like that it isn&#8217;t tied to the models, and that custom validation is very easy.  The only problem that I have found is that it is still relatively undocumented.  Fortunately some brave souls have gone before me and blogged about their experience with newforms and show off some cool tips.  Once such useful blog entry is here: http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/ [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[...] May 30, 2007 at 8:41 am &#183; Filed under Django   I&#8217;ve been exploring Django&#8217;s newforms API.  Which is very sweet.  I feel that it is much improved over the old manipulator system.  I really like that it isn&#8217;t tied to the models, and that custom validation is very easy.  The only problem that I have found is that it is still relatively undocumented.  Fortunately some brave souls have gone before me and blogged about their experience with newforms and show off some cool tips.  Once such useful blog entry is here: <a href="http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/" rel="nofollow">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/</a> [...]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: aaaajusta</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-4393</link>
		<dc:creator>aaaajusta</dc:creator>
		<pubDate>Tue, 08 May 2007 04:55:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-4393</guid>
		<description>&lt;p&gt;thanks for the editpost.html, got it now.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>thanks for the editpost.html, got it now.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Roberto</title>
		<link>http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/comment-page-1/#comment-4340</link>
		<dc:creator>Roberto</dc:creator>
		<pubDate>Sat, 05 May 2007 14:27:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.zoia.org/blog/2007/04/23/using-dynamic-choices-with-django-newforms-and-custom-widgets/#comment-4340</guid>
		<description>&lt;p&gt;Oops... editpost.html was left out by mistake.   It is included now.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Oops&#8230; editpost.html was left out by mistake.   It is included now.</p>]]></content:encoded>
	</item>
</channel>
</rss>
