Tuesday, September 17, 2013

m2m_changed signal not caught


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


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate