Try using a postsave signal. It'll automatically trigger your method
upon saving a Project.

http://docs.djangoproject.com/en/dev/topics/signals/#receiver-functions

Basically, all you'd need is:
from django.db.models.signals import post_save

def member_check(sender, **kwargs):
    #your code

post_save.connect(member_check, sender = Project)

But you should know that on some systems (like mine), it sends the
signal twice per save.

On Sep 23, 4:46 pm, M Godshall <michaelgodsh...@gmail.com> wrote:
> 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