I have a Project model with a ManyToManyField called "members" to keep
track of members of a project.  Whenever the model is updated, I need
to check if certain members need to be removed from or added to the
m2m field.  The most logical place to do this is in the model's custom
save method, but when I try to save the model in the admin the members
field reverts to its previous state even after running
self.members.remove(user1) and self.members.add(user2).  From what I
have researched and tested so far, if you save a project from a front-
end view, the m2m field updates without reverting to its previous
state, but I really need this functionality when a model is saved in
the admin as well.  Any insight into how to make this work would be
appreciated.  Here's what I'm working with right now:

class Project(models.Model):
        ....
        assigned_to = models.ForeignKey(User,
related_name="projects_assigned_to")
        sales_rep = models.ForeignKey(User,
related_name="sales_rep_projects")
        sales_mgr = models.ForeignKey(User,
related_name="sales_mgr_projects")
        ....

        def save(self, force_insert=False, force_update=False):
                if Project.objects.filter(id__exact=self.id).count():
                        project = Project.objects.get(id=self.id)
                        old_sales_rep = project.sales_rep
                        old_sales_mgr = project.sales_mgr
                        super(Project, self).save()
                        if old_sales_rep != self.sales_rep:
                                if old_sales_rep in self.members.all():
                                        self.members.remove(old_sales_rep)
                                if self.sales_rep:
                                        self.members.add(self.sales_rep)
                                if old_sales_rep == self.assigned_to:
                                        new_assigned = self.sales_rep
                        if old_sales_mgr != self.sales_mgr:
                                if old_sales_mgr in self.members.all():
                                        self.members.remove(old_sales_mgr)
                                if self.sales_mgr:
                                        self.members.add(self.sales_mgr)
                                if old_sales_mgr == self.assigned_to:
                                        new_assigned = self.sales_mgr
                        if new_assigned:
                                self.assigned_to = new_assigned
                                super(Project, self).save()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to