title: Why does an error occur when saving the form?
body: <p>There are two models</p>
  
  <p><strong>models1</strong></p>
  
  <pre><code>class Work(models.Model):
  
      employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='employee_projects')
      project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_work')
      ...
      responsibility = models.TextField("employee work responsibility", blank=True)
  </code></pre>
  
  <p><strong>models2</strong></p>
  
  <pre><code>class Project(models.Model):
      name = models.CharField("project name", max_length=64)
      description = models.TextField("project description")
      technologies = models.ManyToManyField(
          Technology, verbose_name="technologies used on the project")
  </code></pre>
  
  <p>I use modeinlineformset to create new objects</p>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class ProjectForm(forms.ModelForm):
      class Meta:
          model = Project
          fields = ['name', 'description', 'technologies']
  
  
  ProjectFormSet = inlineformset_factory(Project, Work,
                                         fields=['responsibility', 'start_year', 'start_month', 'end_year', 'end_month'],
                                         extra=1)
  </code></pre>
  
  <p><strong>and my views.py</strong></p>
  
  <pre><code>class WorkCreateView(AuthorizedMixin, CreateView):
      """
      Create new course instances
      """
      model = Project
      form_class = ProjectForm
      template_name = 'work_edit.html'
  
      def get(self, request, *args, **kwargs):
  
          ....
  
      def post(self, request, *args, **kwargs):
          self.object = None
          form_class = self.get_form_class()
          form = self.get_form(form_class)
          project_form = ProjectFormSet(self.request.POST)
          if form.is_valid() and project_form.is_valid():
              return self.form_valid(form, project_form)
          else:
              return self.form_invalid(form, project_form)
  
      def form_valid(self, form, project_form):
          self.object = form.save()
          project_form.instance = self.object
          project_form.save(commit=False)
          project_form.instance.employee = Employee.objects.get(pk=self.kwargs['pk'])
          project_form.save()
          return redirect('{}#experience'.format(reverse('profile', kwargs={'pk': self.kwargs['pk']})))
  
      def form_invalid(self, form, project_form):
          ...
  </code></pre>
  
  <p>But get an error </p>
  
  <pre><code>null value in column "employee_id" violates not-null constraint
  </code></pre>
  
  <p>I know what this mistake means, but I don't  understand why this happens if I assign a value of the current <code>employee</code> to <code>instance.employee</code>. </p>
  
  <p><strong>UPD</strong></p>
  
  <p>I tried to change views</p>
  
  <pre><code>def form_valid(self, form, project_form):
      self.object = form.save()
      instances = project_form.save(commit=False)
      for instance in instances:
          instance.employee = Employee.objects.get(pk=self.kwargs['pk'])
          instance.save()
      return redirect('{}#experience'.format(reverse('profile', kwargs={'pk': self.kwargs['pk']})))
  </code></pre>
  
  <p>But got new error</p>
  
  <pre><code>save() prohibited to prevent data loss due to unsaved related object 'project'.
  </code></pre>