title: M2M field widget to add or update data as inline formset
body: <p>I spent a lot of hours searching for a feature which I think should be quite a basic functionality in Django. But I just can't get it working,</p>
  
  <p>I am unable to find a widget which will function same as m2m widget of django, but will also create new model instance if it doesn't exists.</p>
  
  <p><strong>Note:</strong> Here model instance already exists means that data entered in inline widget already exists in database.</p>
  
  <p>E.g.
  If I had models as:</p>
  
  <pre><code>class Outcome(models.Model):
      outcome = models.CharField(max_length=255)
      outcome_short_name = models.CharField(max_length=10, blank=True, null=True)
  
  
  class Course(models.Model):
      course_title = models.CharField(
          verbose_name=COURSE_SINGULAR + " title", max_length=200, unique=True
      )
  
      course_outcome = models.ManyToManyField(
          Outcome, verbose_name=COURSE_SINGULAR + " outcome", blank=True
      )
  </code></pre>
  
  <p>Then I want "Outcomes" shown as this image while creating course:
  <a href="https://i.stack.imgur.com/77wrv.png" rel="nofollow noreferrer">Image of adding new course with inline outcomes</a></p>
  
  <p>Now, If the outcomes data added by user already exists, then it should only map them to course. Otherwise it should first store outcomes into database and then map them to course.</p>
  
  <p>Any guidance in right direction will be highly appreciated.</p>
  
  <p>Thanks,
  <br><br></p>
  
  <p><strong>EDIT:</strong>
  As suggested by @dirkgroten to use modelformset, I changed my FormView as:</p>
  
  <pre><code>class CourseFormView(FormView):
      template_name = "course/course_form.html"
      form_class = CourseForm
      success_url = "/admin/"
  
      def get_context_data(self, **kwargs):
          context = super(CourseFormView, self).get_context_data(**kwargs)
          if self.request.POST:
              context["outcomes"] = OutcomeFormSet(self.request.POST)
          else:
              context["outcomes"] = OutcomeFormSet(queryset=Outcome.objects.none())
          return context
  
      def form_valid(self, form, **kwargs):
          super(CourseFormView, self).get_context_data(**kwargs)
          context = self.get_context_data()
          outcomes_formset = context["outcomes"]
          if not outcomes_formset.is_valid():
              return super().form_invalid(form)
  
          cleaned_data = form.cleaned_data
          cleaned_data.pop("course_outcome")
          course = Course.objects.create(**cleaned_data)
          course.save()
  
          outcomes_formset.instance = course
          outcomes_formset.save()
  
  
  course.course_outcome.set(Outcome.objects.filter(course_outcome=course))
      return super().form_valid(form)
  </code></pre>
  
  <p>Everything looks fine except my model_formset is not validated if form data in formset already exists in database.<br>
  E.g. if I enter (outcome="test_outcome", outcome_short_name="test_short") in formset and same data already exists in outcome table, then my formset gives error:<br>
  Outcome with this Outcome and Outcome short name already exists.</p>
  
  <p>Is there any way to tackle this situation or I am doing something wrong.</p>
  
  <p>You can test above at: <a href="http://code.gdy.club:8001/course/add/" rel="nofollow noreferrer">http://code.gdy.club:8001/course/add/</a><br>
  outcomes_list: <a href="http://code.gdy.club:8001/outcome/" rel="nofollow noreferrer">http://code.gdy.club:8001/outcome/</a></p>
  
  <p>Thanks,</p>
  
  <p>--<br>
  Suraj<br>
  <a href="https://hacksj4u.wordpress.com" rel="nofollow noreferrer">https://hacksj4u.wordpress.com</a><br>
  <a href="https://github.com/SurajDadral" rel="nofollow noreferrer">https://github.com/SurajDadral</a></p>