title: Django - How to save m2m data via post_save signal?
body: <p>(Django 1.1) I have a Project model that keeps track of its members using a m2m field.  It looks like this:</p>
  
  <pre><code>class Project(models.Model):
      members = models.ManyToManyField(User)
      sales_rep = models.ForeignKey(User)
      sales_mgr = models.ForeignKey(User)
      project_mgr = models.ForeignKey(User)
      ... (more FK user fields) ...
  </code></pre>
  
  <p>When the project is created, the selected <code>sales_rep</code>, <code>sales_mgr</code>, <code>project_mgr</code>, etc <code>User</code>s are added to members to make it easier to keep track of project permissions.  This approach has worked very well so far.</p>
  
  <p>The issue I am dealing with now is how to update the project's membership when one of the <code>User</code> FK fields is updated via the admin.  I've tried various solutions to this problem, but the cleanest approach seemed to be a <code>post_save</code> signal like the following:</p>
  
  <pre><code>def update_members(instance, created, **kwargs):
      """
      Signal to update project members
      """
      if not created: #Created projects are handled differently
          instance.members.clear()
  
          members_list = []
          if instance.sales_rep:
              members_list.append(instance.sales_rep)
          if instance.sales_mgr:
              members_list.append(instance.sales_mgr)
          if instance.project_mgr:
              members_list.append(instance.project_mgr)
  
          for m in members_list:
              instance.members.add(m)
  signals.post_save.connect(update_members, sender=Project)  
  </code></pre>
  
  <p>However, the <code>Project</code> still has the same members even if I change one of the fields via the admin!  I have had success updating members m2m fields using my own views in other projects, but I never had to make it play nice with the admin as well.  </p>
  
  <p>Is there another approach I should take other than a post_save signal to update membership?  Thanks in advance for your help!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Just to clarify, the post_save signal works correctly when I save my own form in the front end (old members are removed, and new ones added).  However, the post_save signal does NOT work correctly when I save the project via the admin (members stay the same).  </p>
  
  <p>I think Peter Rowell's diagnosis is correct in this situation.  If I remove the "members" field from the admin form the post_save signal works correctly.  When the field is included, it saves the old members based on the values present in the form at the time of the save.  No matter what changes I make to the members m2m field when project is saved (whether it be a signal or custom save method), it will always be overwritten by the members that were present in the form prior to the save.  Thanks for pointing that out!</p>