title: Django save related ManyToMany model
body: <p>I have two models with ManyToMany relationship as this:</p>
  
  <pre><code>class Subject(models.Model):
      teachers = models.ManyToManyField(
          Teacher
      )
      subject = models.CharField(
         'Materia',
         max_length = 50,
         unique = True
     )
     level = models.CharField(
         'Nivel',
         max_length = 3,
         choices = LEVEL_CHOICES
     )
  
  class Teacher(Person):
      # fields definition
  </code></pre>
  
  <p><a href="https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/" rel="nofollow noreferrer">Following the docs</a> I know I can save Subject model without saving Teacher field. That's fine.</p>
  
  <p><strong>Now I want to add a Subject to Teacher, so I need a form with selected teacher, and a choice field with all the subjects.</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/32998300/django-createview-how-to-perform-action-upon-save">I found this question</a> that relates to what I want to accomplish, but I think it doesn't address what I'm looking for. </p>
  
  <p>First I populate the subject choice filed, as shown in the ModelForm code below.</p>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class SubjectTeacherForm(ModelForm):
      subject = forms.ChoiceField(choices=[(m.id, m.subject) for m in Subject.objects.all()])
  
      class Meta:
          model = Subject
          fields = ['teachers', 'subject']
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>#here I send the teacher id to the view
  url(r'^materias/asignar/profesor/(?P&lt;teacher_id&gt;\d+)/$',MateriaTeacherCreateView.as_view(), name='materia-create-teacher'),
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class SubjectTeacherCreateView(SuccessMessageMixin, CreateView):
      model = Subject
      template_name = 'edu/subject/subject_create.html'
      form_class = SubjectTeacherForm
  
      def form_valid(self, form):
          # here I want to add the relation
          teacher = Teacher.objects.get(pk=self.kwargs['teacher_id'])
          # I understad that object here is the ModelForm, not the Model, so this is wrong
          self.object.teachers.add(teacher)
          return redirect(self.get_success_url())
  
      def get_context_data(self, **kwargs):
          context = super(SubjectTeacherCreateView, self).get_context_data(**kwargs)
          # here I define the selected teacher to pass to the template
          context['teacher'] = Teacher.objects.get(pk=self.kwargs['teacher_id'])
          return context
  </code></pre>
  
  <p><strong>the template</strong></p>
  
  <pre><code>&lt;form method="post" class="form-horizontal"&gt;
      {% csrf_token %}
      {{ form.teacher }}
      {{ form.subject }}
  </code></pre>
  
  <p>As expected, this is not working, the form doesn't save, it throws the error:</p>
  
  <pre><code>AttributeError at /edu/materias/asignar/profesor/11/
  'NoneType' object has no attribute 'teachers'
  </code></pre>
  
  <p>So, obviously I'm doing something wrong.</p>
  
  <p>My question is if I can use class based views to accomplish this, or I should write a method to save the relationship, and what should I change in my code. </p>
  
  <p>I expect I made myself clear enough, thanks.</p>