Haven't tested it, but the "pre_add" and "pre_remove" actions in the m2m_changed signal should be triggered before the change, and they include a pk_set arg with the id's of the related M2M model being added / removed.
You could also link to the post_save and post_remove which contain the same flag, but that depends on what are you trying to accomplish.
On Sun, Mar 8, 2015 at 8:15 PM, Fabien Schwob <xphuture@gmail.com> wrote:
With the m2m changed signal I can only have the state of the object after it has been saved. Not the difference between before and after the change.--On sam. 7 mars 2015 at 16:53 Vijay Khemlani <vkhemlan@gmail.com> wrote:To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei0-ZNc2je48jNdt_E78jK%2BO-HCO8zpKyb3%2BtCMx0Ee9Xw%40mail.gmail.com.You might find the m2m_changed signal usefulOn Sat, Mar 7, 2015 at 7:57 AM, Fabien Schwob <xphuture@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+unsubscribe@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.
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+unsubscribe@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.
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+unsubscribe@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%3DxoZQbykrPHhdeo8vVr2rD0R4owVhP%2Bd7OZ9fCux4qqStVNg%40mail.gmail.com.
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+unsubscribe@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/CALn3ei0DMpjtp0qdf1nFh%3Dht8j2DY7urrEx6fHytMLxshAwihA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment