title: Displaying a subset of data with Django ModelChoiceField and bound data
body: <p>When using ModelChoiceField in Django forms I only want a subset of the data (in my case the last 100 entries) to be rendered, since the data set is very large. At the same time I want to the user to still be able to choose from any object in the database, not just the ones displayed by default (I'm using AJAX to query for options not displayed by default).</p>
  
  <p>I have managed to do this by subclassing ModelChoiceField and ModelChoiceIterator.</p>
  
  <pre><code>class SubsetModelChoiceIterator(ModelChoiceIterator):
      def __init__(self, field):
          self.field = field
          self.queryset = field.subset_queryset
  
  class SubsetModelChoiceField(forms.ModelChoiceField):
      """
      This is just like a ModelChoiceField, but only a subset of the full
      queryset will be displayed as choices.
      """
  
      def __init__(self, subset_queryset, *args, **kwargs):
          self.subset_queryset = subset_queryset
          super(SubsetModelChoiceField, self).__init__(*args, **kwargs)
  
      def _get_choices(self):
          if hasattr(self, '_choices'):
              return self._choices
  
          return SubsetModelChoiceIterator(self)
      choices = property(_get_choices, forms.ChoiceField._set_choices)
  
      def _get_subset_queryset(self):
          return self._subset_queryset
  
      def _set_subset_queryset(self, queryset):
          self._subset_queryset = queryset
          self.widget.choices = self.choices
  </code></pre>
  
  <p>When defining a form field I am now able to supply 2 querysets, one that the submitted data will be validated against and the other that will be displayed.</p>
  
  <pre><code>class MyForm(Form):
      some_option = SubsetModelChoiceField(queryset=MyModel.objects.all(),
                                           subset_queryset=MyModel.objects.all()[:100],)
  </code></pre>
  
  <p>This is working fine, however, a usability problem arises.
  When a user selects an option not displayed by default, but another form field causes the bound form to fail validation, then on page reload the option the user selected will not be pre-selected, as it's not found in the "subset_queryset".</p>
  
  <p>Does anyone have an idea how I could solve this? </p>
  
  <p>Thanks.  </p>