title: Django with Select2 remote data example
body: <p>I have a model (let's call it <code>Animal</code>) that has a <code>ForeignKey</code> to a <code>Genus</code> model. I would like to have a <code>Modelform</code> that allows the user to select an Animal instance.  Clearly, there are too many animals for a single select list so I would like the user to be able to filter by genus and then search by animal.</p>
  
  <p><a href="https://select2.github.io" rel="nofollow">Select2</a> has <a href="https://select2.github.io/examples.html#data-ajax" rel="nofollow">an example of using Ajax request</a> to do something oh-so-similar to what I want.  Instead of searching for a <em>usr/repo</em>, I would search for a <em>genus/animal</em>. The user might not know the exact genus or the exact animal they want, so both bits need to be searchable. This UI is fine for my needs.</p>
  
  <p><a href="https://github.com/applegrew/django-select2" rel="nofollow">Django-select2</a> allows me to use <a href="https://select2.github.io" rel="nofollow">Select2</a> with Django and has a <a href="http://django-select2.readthedocs.org/en/latest/django_select2.html#django_select2.forms.HeavySelect2Widget" rel="nofollow">widget</a> that I think I should be able to use. Note that <code>urls.py</code> contains a <code>url(r'^heavy_data/$', heavy_data, name='heavy_data'),</code> so I can access the view method fine. </p>
  
  <p>In my <code>ModelForm</code>'s <code>__init__(...)</code>, have:</p>
  
  <pre><code>self.fields['animal'] = forms.ModelChoiceField(
      widget=HeavySelect2Widget(data_view='heavy_data'),
      queryset=Animal.objects.all())
  </code></pre>
  
  <p>And in the <code>views</code>:</p>
  
  <pre><code>def heavy_data(request):
      filtered_animals = {}
      if request.is_ajax():
          import ipdb
          ipdb.set_trace()
          # What next?
      return HttpResponse(json.dumps(filtered_animals))
  </code></pre>
  
  <p>I am unsure how to pass the whole <code>genus/animal</code> search string form the form to the view. However, I might not even be on the right tracks... Am I? Is there a better way to do what I want?</p>