title: Django - How to make a form for a model's foreign keys?
body: <p>Here's what I'm trying to do.  I'm wondering if someone can suggest a good approach:</p>
  
  <p>models.py:</p>
  
  <pre><code>class Color(models.Model):
      name = models.CharField(...
  
  class Speed(models.Model):
      name = models.CharField(...
  
  class Dog(models.Model):
      name = models.CharField(...
      color = models.ForeignKey(Color...
      speed = models.ForeignKey(Speed...
  
  class DogRequest(models.Model):
      dog = models.ForeignKey(Dog...
      request_time = models.DateTimeField()
  </code></pre>
  
  <p>Now I want to have a page where a user can enter or edit a DogRequest, but the form should look like this:</p>
  
  <pre><code>Enter Dog Request:
  ---------------------
  color (dropdown V)
  speed (dropdown V)
  |save|
  </code></pre>
  
  <p>So how would I design that form in the view?  The problem is the user will be entering properties of a dog, but not a dog.  So I need to find or create a dog record depending on what the user enters.</p>
  
  <p>So far I've tried something like this:</p>
  
  <pre><code>class DogRequestForm(ModelForm):
      class Meta:
          model = DogRequest
          fields = ('request_time','color','speed')
      color = forms.ModelChoiceField(queryset=Color.objects.all())
      speed = forms.ModelChoiceField(queryset=Speed.objects.all())
  </code></pre>
  
  <p>Now assuming that even works, where do I put the code to figure out the Dog record to use (or create) depending on what's entered for color and speed?  And how do I fill the form from a DogRequest?</p>