title: Django ManyToMany relationship not persisting
body: <p>I've encountered a nuisance in a Django application on which I'm working where when a user attempts to create a many-to-many relationship then that relationship does not persist. I can create such relationships via the admin interface, though.</p>
  
  <p>Here's some code which I hope might explain it.</p>
  
  <p>models.py:</p>
  
  <pre><code>class Foo(models.Model):
      ...
      linked_bar = models.ManyToManyField(Bar, blank=True)
      ...
  </code></pre>
  
  <p>views/foo.py:</p>
  
  <pre><code>def editFoo(request,foo_id):
      fooQuery = Foo.objects.filter(id=foo_id)
      if len(fooQuery) &gt; 0:
         foo = fooQuery.get(id=foo_id)
      else:
          # handle error &amp;c.
      ...
      print str(len(foo.linked_bar.all())) # prints '0'
      linked_bar_id = request.POST.get('linked-bar-id', '')
      barQuery = Bar.objects.filter(id=linked_bar_id)
      if len(barQuery) &gt; 0:
         bar = barQuery.get(id=bar_id)
         foo.linked_bar.add(bar)
         print str(len(foo.linked_bar.all())) # prints '1'
      return redirect("/foo/" + foo_id) # calls showFoo()
  
  def showFoo(request,foo_id):
      fooQuery = Foo.objects.filter(id=foo_id)
      if len(fooQuery) &gt; 0:
         foo = fooQuery.get(id=foo_id)
      ...
      print str(len(foo.linked_bar.all())) # prints '0'
  </code></pre>
  
  <p>So, the ManyToMany relationship appears to be being created in foo.py during the edit stage, but doesn't persist to where it should be shown to the user. Can anyone suggest what might be happening here, or where I might look for further clues?</p>