title: How do I filter ForeignKey choices in a Django ModelForm?
body: <p>Say I have the following in my <code>models.py</code>:</p>
  
  <pre><code>class Company(models.Model):
     name = ...
  
  class Rate(models.Model):
     company = models.ForeignKey(Company)
     name = ...
  
  class Client(models.Model):
     name = ...
     company = models.ForeignKey(Company)
     base_rate = models.ForeignKey(Rate)
  </code></pre>
  
  <p>I.e. there are multiple <code>Companies</code>, each having a range of <code>Rates</code> and <code>Clients</code>.  Each <code>Client</code> should have a base <code>Rate</code> that is chosen from it's parent <code>Company's Rates</code>, not another <code>Company's Rates</code>.</p>
  
  <p>When creating a form for adding a <code>Client</code>, I would like to remove the <code>Company</code> choices (as that has already been selected via an "Add Client" button on the <code>Company</code> page) and limit the <code>Rate</code> choices to that <code>Company</code> as well.</p>
  
  <p>How do I go about this in Django 1.0?  </p>
  
  <p>My current <code>forms.py</code> file is just boilerplate at the moment:</p>
  
  <pre><code>from models import *
  from django.forms import ModelForm
  
  class ClientForm(ModelForm):
      class Meta:
          model = Client
  </code></pre>
  
  <p>And the <code>views.py</code> is also basic:</p>
  
  <pre><code>from django.shortcuts import render_to_response, get_object_or_404
  from models import *
  from forms import *
  
  def addclient(request, company_id):
      the_company = get_object_or_404(Company, id=company_id)
  
      if request.POST:
          form = ClientForm(request.POST)
          if form.is_valid():
              form.save()
              return HttpResponseRedirect(the_company.get_clients_url())
      else:
          form = ClientForm()
  
      return render_to_response('addclient.html', {'form': form, 'the_company':the_company})
  </code></pre>
  
  <p>In Django 0.96 I was able to hack this in by doing something like the following before rendering the template:</p>
  
  <pre><code>manipulator.fields[0].choices = [(r.id,r.name) for r in Rate.objects.filter(company_id=the_company.id)]
  </code></pre>
  
  <p><a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to" rel="noreferrer"><code>ForeignKey.limit_choices_to</code></a> seems promising but I don't know how to pass in <code>the_company.id</code> and I'm not clear if that will work outside the Admin interface anyway.</p>
  
  <p>Thanks.  (This seems like a pretty basic request but if I should redesign something I'm open to suggestions.)</p>