title: Django automatically set foreign key in form (with class based views)
body: <p>I want to use a form to generate a new object (say a Book) from a Person's page such that the new Book is automatically associated with that Person via a foreign key, but I am running into trouble getting the Person correctly associated and saved with the form. For my models, I have:</p>
  
  <pre><code>class Person(models.Model):
      p_id = models.PositiveIntegerField(primary_key=True, unique=True)
  
  class Book(models.Model):
      person = models.ForeignKey(Person)
      title = models.CharField(max_length=100)
  </code></pre>
  
  <p>I then have a custom form to create a Book:</p>
  
  <pre><code>class AddBookForm(forms.ModelForm):
      class Meta:
          model = Book
          fields = ('title', 'person',)
          widgets = {
              'person': forms.HiddenInput,
          }
  </code></pre>
  
  <p>And a view to create the Book (which is also given the <code>pk</code> (<code>p_id</code>) of the Person):</p>
  
  <pre><code>class AddBook(CreateView):
      model = Book
  </code></pre>
  
  <p>I have tried various things to get this to work:</p>
  
  <ul>
  <li>Pre-populating the Person field with <code>get_initial</code>, but this disappears for some reason with the input is set to hidden (frustratingly).</li>
  <li>modifying the <code>form_valid</code> method in the view, but this only occurs after the form has already been validated, so I need to add Person before that.</li>
  <li>modifying the <code>clean_person</code> method in the form class, but that only occurs after the form has been validated by the <code>clean</code> method.</li>
  </ul>
  
  <p>Currently, I am trying to override the <code>clean</code> method. However, I don't know how to get the Person at this point, after the form has already been sent for cleaning. In the view, I could access it with <code>Patient.objects.get(p_id=self.kwargs.get('pk'))</code>.</p>
  
  <p>Is there some way I can add the data to the form in my view (as a class-based view) that it won't get stripped away <strong>OR</strong> is there some way I can access the Person or <code>p_id</code> foreign key at this point to add the data in the <code>clean</code> method?</p>