title: How to limit the choices of a child field based on a parent field's value?
body: <p>I have several rather complex forms which rely on model hierarchies to assign <code>ForeignKey</code> values. I need to limit the choices available to a child field based upon the value of its parent field. The problem is that the value of a parent field can come from initial data (a GET request) <em>or</em> from bound data (a POST request). Thus, I end up replicating this pattern a lot:</p>
  
  <pre><code>class MyForm(forms.Form):
      parent = forms.ModelChoiceField(queryset=Parent.objects.all())
      child = forms.ModelChoiceField(queryset=Child.objects.all())
  
      def __init__(self, *args, **kwargs):
          super(MyForm, self).__init__(*args, **kwargs)
  
          # Limit 'child' choices based on current value of 'parent'
          if self.is_bound and self.data.get('parent'):
              self.fields['child'].queryset = Child.objects.filter(parent__pk=self.data['parent'])
          elif self.initial.get('parent'):
              self.fields['child'].queryset = Child.objects.filter(parent=self.initial['parent'])
          else:
              self.fields['child'].choices = []
  </code></pre>
  
  <p>I get the feeling there's a more efficient way to accomplish this. What am I missing?</p>