m2m_changed signal not caught

97 views
Skip to first unread message

Roberto López López

unread,
Sep 17, 2013, 6:59:48 AM9/17/13
to django...@googlegroups.com

Hi,

I need to use the m2m_changed signal to assign permissions over an object. But somehow, I cannot make it work.

To summarise, my models.py is as follows:

from cmsplugin_news.models import News
from django.contrib.auth.models import Group
from django.db import models
from guardian.shortcuts import assign_perm, remove_perm


class Department(models.Model):
    news = models.ManyToManyField(
        News, blank=True, related_name='departments'
    )
    write_group = models.ForeignKey(Group, blank=True)


@receiver(m2m_changed, sender=Department.news.through)
def _m2m_changed_news_departments(sender, instance, action, reverse, model, pk_set, **kwargs):
    if action == "pre_add":
        for pk in pk_set:
            d = Department.objects.get(pk__exact=pk)
            assign_perm('cmsplugin_news.change_news', d.write_group, instance)
            assign_perm('cmsplugin_news.delete_news', d.write_group, instance)
    elif action == "pre_remove":
        for pk in pk_set:
            d = Department.objects.get(pk__exact=pk)
            remove_perm('cmsplugin_news.change_news', d.write_group, instance)
            remove_perm('cmsplugin_news.delete_news', d.write_group, instance)

The point is that the signal is never caught. I am editing the m2m relation from the admin interface (as an inline in cmsplugin_news). When I add/delete a department from the formset, the signal is never caught by my method.

Any help here? Thank you very much.

Roberto


John DeRosa

unread,
Sep 17, 2013, 11:02:59 AM9/17/13
to django...@googlegroups.com
From memory, I _think_ this catches the signal from the Department model, not the News model. To catch it when you edit the relationship from a *News* model form, you need to hook it up ("sender=News.department.through"). 

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Roberto López López

unread,
Sep 17, 2013, 11:30:44 AM9/17/13
to django...@googlegroups.com

Thanks for your advice, but that dint't solve the issue :-/

@receiver(m2m_changed, sender=News.department.through)
-- 

Roberto López López
System Developer
Parallab, Uni Computing
+47 55584091
Reply all
Reply to author
Forward
0 new messages