You might find the m2m_changed signal useful

https://docs.djangoproject.com/en/1.7/ref/signals/#m2m-changed

On Sat, Mar 7, 2015 at 7:57 AM, Fabien Schwob <xphut...@gmail.com> wrote:

> Hello,
>
> I would like to detect changes in a ManyToMany field, to update data
> in the linked Model. I've two objects :
>
> class Category(models.Model):
>     .... some fields ...
>
>
> class Post(models.Model):
>     .... some fields ...
>     categories = models.ManyToManyField(Category)
>
> And I would like to detect (in the admin and when accessing the object
> in python) when categories are added or removed to a Post. To update
> some fields in the Category model. But I've tried with :
>
>  * django.db.models.signals.m2m_changed, but it doesn't appear to be
> trigged in the admin
>  * by overriding Post.save(), but I get the same information before
> and after the save() (I think it's because m2m are handled after
> save() has finished) :
>
> def save(self, *args, **kwargs):
>         print Post.objects.get(pk=self.id).categories.all()
>         super(Post, self).save(*args, **kwargs)
>         print Post.objects.get(pk=self.id).categories.all()
>
> Any solutions to handle this case ? With a custom signal, or anything else
> ?
>
> Thanks
> --
> Fabien Schwob
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAL%3DxoZRnd7812MykJSsh3Avhh%2BiFk9J3g9nkhv61UA%3D4a9j8Ow%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAL%3DxoZRnd7812MykJSsh3Avhh%2BiFk9J3g9nkhv61UA%3D4a9j8Ow%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei2LSkjKON527S_M3JqJbRTEw8UNtmAdfreGFpiu2X6%2BqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to