<?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: Runtime ChoiceField filtering in Django&#8217;s admin</title>
	<atom:link href="http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Tue, 07 Sep 2010 20:27:19 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: AJ</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-2064</link>
		<dc:creator>AJ</dc:creator>
		<pubDate>Fri, 11 Jun 2010 19:13:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-2064</guid>
		<description>Also you would need to assign the form and formset to the inline.

admin.py
&lt;code&gt;
class BilletInline(admin.StackedInline):
    model = Billet
    extra = 1
    form = BilletAdminForm
    formset = BilletAdminFormSet
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Also you would need to assign the form and formset to the inline.</p>
<p>admin.py<br />
<code><br />
class BilletInline(admin.StackedInline):<br />
    model = Billet<br />
    extra = 1<br />
    form = BilletAdminForm<br />
    formset = BilletAdminFormSet<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AJ</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-2063</link>
		<dc:creator>AJ</dc:creator>
		<pubDate>Fri, 11 Jun 2010 19:11:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-2063</guid>
		<description>The only way I could think to handle a formset is to sublcass django.forms.models.BaseInlineFormSet, override the _construct_form method and pass in some an additional kwarg(the parent instance) that will then get passed to the form.  So you would need to then override the __init__ method in the form and use the extra kwarg to change the queryset on the field.  The pseudo code would be something like:

&lt;code&gt;
class BilletAdminFormSet(BaseInlineFormSet):
  def _construct_form(self, i, **kwargs):
    kwargs.update({&#039;instance&#039;: self.instance})
    return super(BilletAdminFormSet, self)._construct_form(i, **kwargs)

class BilletAdminForm(CustomModelForm):
  ...
  def __init__(self, *args, **kwargs):
    super(BilletAdminForm, self).__init__(*args, **kwargs)
    if &#039;instance&#039; in kwargs and kwargs[&#039;instance&#039;]:
            self.fields[&#039;employees&#039;].queryset = SomeQuerySet()
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>The only way I could think to handle a formset is to sublcass django.forms.models.BaseInlineFormSet, override the _construct_form method and pass in some an additional kwarg(the parent instance) that will then get passed to the form.  So you would need to then override the __init__ method in the form and use the extra kwarg to change the queryset on the field.  The pseudo code would be something like:</p>
<p><code><br />
class BilletAdminFormSet(BaseInlineFormSet):<br />
  def _construct_form(self, i, **kwargs):<br />
    kwargs.update({'instance': self.instance})<br />
    return super(BilletAdminFormSet, self)._construct_form(i, **kwargs)</p>
<p>class BilletAdminForm(CustomModelForm):<br />
  ...<br />
  def __init__(self, *args, **kwargs):<br />
    super(BilletAdminForm, self).__init__(*args, **kwargs)<br />
    if 'instance' in kwargs and kwargs['instance']:<br />
            self.fields['employees'].queryset = SomeQuerySet()<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Wall</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-2009</link>
		<dc:creator>Andrew Wall</dc:creator>
		<pubDate>Mon, 22 Feb 2010 16:40:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-2009</guid>
		<description>Thanks very much for writing about this.  I&#039;m new to both python and django and there didn&#039;t seem to be a way to handle this kind of problem without abandoning the Admin screens from the django doc.</description>
		<content:encoded><![CDATA[<p>Thanks very much for writing about this.  I&#8217;m new to both python and django and there didn&#8217;t seem to be a way to handle this kind of problem without abandoning the Admin screens from the django doc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andri Jan</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-1709</link>
		<dc:creator>Andri Jan</dc:creator>
		<pubDate>Mon, 03 Aug 2009 13:02:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-1709</guid>
		<description>Yeah, that&#039;s what I&#039;d like to know. This is no problem with a form, it&#039;s the formset that&#039;s the problem</description>
		<content:encoded><![CDATA[<p>Yeah, that&#8217;s what I&#8217;d like to know. This is no problem with a form, it&#8217;s the formset that&#8217;s the problem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-1705</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Sat, 01 Aug 2009 03:49:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-1705</guid>
		<description>That&#039;s a good point. How would that work with a form set, though? I didn&#039;t see that the parent instance is passed to the form set&#039;s constructor.</description>
		<content:encoded><![CDATA[<p>That&#8217;s a good point. How would that work with a form set, though? I didn&#8217;t see that the parent instance is passed to the form set&#8217;s constructor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-1704</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Fri, 31 Jul 2009 21:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-1704</guid>
		<description>You might also do something like:

&lt;pre&gt;
class RunTimeGameForm(forms.ModelForm):
    [...]
    def __init__(*args, **kwargs):
        super(RunTimeGameForm, self).__init__(*args, **kwargs)
        if self.instance.id:
            self.fields[&#039;home_team&#039;].queryset=Team.objects.filter(
                sport=self.instance.season.sport)
&lt;/pre&gt;

Then there&#039;s no need to override ModelAdmin.get_formset, or have a game_form_factory function.</description>
		<content:encoded><![CDATA[<p>You might also do something like:</p>
<pre>
class RunTimeGameForm(forms.ModelForm):
    [...]
    def __init__(*args, **kwargs):
        super(RunTimeGameForm, self).__init__(*args, **kwargs)
        if self.instance.id:
            self.fields['home_team'].queryset=Team.objects.filter(
                sport=self.instance.season.sport)
</pre>
<p>Then there&#8217;s no need to override ModelAdmin.get_formset, or have a game_form_factory function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Grant</title>
		<link>http://www.artfulcode.net/articles/runtime-choicefield-filtering-in-djangos-admin/comment-page-1/#comment-1703</link>
		<dc:creator>David Grant</dc:creator>
		<pubDate>Fri, 31 Jul 2009 14:42:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.artfulcode.net/?p=741#comment-1703</guid>
		<description>I did this last year...I think some guy on the mailing list gave me the code. I&#039;m glad to see a blog post about this, because there was none at the time so it took me forever to find the solution. This is one of those things that I think a lot of people need at some point in Django development whenever the models get more complex like this and it&#039;s not exactly obvious how to do it. Thanks!</description>
		<content:encoded><![CDATA[<p>I did this last year&#8230;I think some guy on the mailing list gave me the code. I&#8217;m glad to see a blog post about this, because there was none at the time so it took me forever to find the solution. This is one of those things that I think a lot of people need at some point in Django development whenever the models get more complex like this and it&#8217;s not exactly obvious how to do it. Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
