title: Django: Set initial value for ForiegnKey in a CreateView
body: <p>I'm trying to create a view of type <code>CreateView</code>. This view will take the <code>form_class = CourseForm</code> that I created and excluded some fields in it. The <code>instructor</code> field is a foriegnKey and I don't want the user to be able to control it in the form. It's a field that depends on the signed in user.</p>
  
  <pre><code># forms.py
  class CourseForm(forms.ModelForm):
      class Meta:
          model = Course
          exclude = ['instructor', 'members', 'slug']
      # ...
  </code></pre>
  
  <p>my view is as follows. I thought that by including the instructor value in <code>initial</code> would pass the profile instance when I submit</p>
  
  <pre><code># views.py
  
  @method_decorator(login_required, name='dispatch')
  class CourseCreateView(CreateView):
      model = Course
      template_name = 'course_form.html'
      success_url = reverse_lazy('course-create-complete')
  
      form_class = CourseForm
  
      def get_initial(self):
          initial = super(CourseCreateView, self).get_initial()
          initial = initial.copy()
          profile = get_object_or_404(Profile, user__username=self.request.user)
          initial['instructor'] = profile
          return initial
  
  
  # models.py
  
  class Course(models.Model):
      instructor = models.ForeignKey(Profile, related_name="Instructor")
      # ... other fields
  </code></pre>
  
  <p>but the probelm is that whenever I submit the form I get the following error:</p>
  
  <blockquote>
    <p>NOT NULL constraint failed: myapp_course.instructor_id</p>
  </blockquote>