title: Django Forms: Foreign Key in Hidden Field
body: <p>My form:</p>
  
  <pre><code>class PlanForm(forms.ModelForm):    
      owner = forms.ModelChoiceField(label="",
                                    queryset=Profile.objects.all(),
                                    widget=forms.HiddenInput())
      etc...
  
      class Meta:
          model = Plan
  </code></pre>
  
  <p>Owner, in the model, is a ForeignKey to a Profile.</p>
  
  <p>When I set this form, I set the value of "owner" to be a Profile object.</p>
  
  <p>But when this comes out on the form, it seems to contain the <em>name</em> of the Profile like this:</p>
  
  <pre><code>&lt;input type="hidden" name="owner" value="phil" id="id_owner" /&gt;
  </code></pre>
  
  <p>When the form is submitted and gets back to my views.py I try to handle it like this:</p>
  
  <pre><code>    form = PlanForm(request.POST)
      ...
      if form.is_valid():                
          plan = form.save()
          return HttpResponseRedirect('/plans/%s'%plan.id) # Redirect after POST
  </code></pre>
  
  <p>However, what I get is a type-conversion error as it fails to turn the string "phil" (the user's name that was saved into the "owner" field) into an Int to turn it into the ForeignKey.</p>
  
  <p>So what is going on here. Should a ModelForm represent a foreign key as a number and transparently handle it? Or do I need to extract the id myself into the owner field of the form? And if so, how and when do I map it back BEFORE I try to validate the form? </p>