The presave signal passes the instance of the updated object to the
signal automatically, so your code should work with minimal
modifications.

Try this:

from django.db.models.signals import post_save

def member_check(sender, **kwargs):
    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
                        new_pro = kwargs['instance']
                        if old_sales_rep != new_pro.sales_rep:
                                if old_sales_rep in new_pro.members.all
():
                                        new_pro.members.remove
(old_sales_rep)
                                if new_pro.sales_rep:
                                        new_pro.members.add
(new_pro.sales_rep)
                                if old_sales_rep ==
new_pro.assigned_to:
                                        new_assigned =
new_pro.sales_rep
                        if old_sales_mgr != new_pro.sales_mgr:
                                if old_sales_mgr in new_pro.members.all
():
                                        new_pro.members.remove
(old_sales_mgr)
                                if new_pro.sales_mgr:
                                        new_pro.members.add
(new_pro.sales_mgr)
                                if old_sales_mgr ==
new_pro.assigned_to:
                                        new_assigned =
new_pro.sales_mgr
                        if new_assigned:
                                new_pro.assigned_to = new_assigned

post_save.connect(member_check, sender = Project)

On Sep 24, 3:23 pm, "Daniele Procida" <dani...@apple-juice.co.uk>
wrote:
> On Wed, Sep 23, 2009, 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.
>
> I am trying to do exactly the same thing as you (see the concurrent
> thread "verriding save() for ManyToManyFields").
>
> I am not having much luck.
>
> So far I have ascertained that m2m fields *don't* need otb esaved to be
> updated, but, like you:
>
> > 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).
>
> It appears that they have changed, but the changes don't seem to get to
> the database.
>
> > 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.
>
> I have not seen anything to suggest that an admin save won't trigger
> this, but whether I use a post_save signal, or try to do it in the save
> () override, it fails to stick.
>
> I really can't work out why it's failing to stick.
>
> Daniele
--~--~---------~--~----~------------~-------~--~----~
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