I need to detect a post_remove signal, so I have written :
def handler1(sender, instance, action, reverse, model, pk_set, **kwargs):
if (action == \'post_rem
I had reach a conclusion after long long time o seaching First my problem: I had some how update one atribute from my model to set it False when my m2m is is empty, and true if it have at least 1 item, so, the true thing works but when i try to "pre_remove" or "post_remove" never is trigged, so after some trys with some differents examples i saw something weird on this "pre_clear", each time i change my m2m this always has the last values, so i manage to force delete this values from my m2m and this way it trigger the pre_remove and post_remove, so this works for me, see bellow the code
So now i can automaticly set ativo True or False based on my m2m
class Servico(BaseMixin):
descricao = models.CharField(max_length=50)
#This inheritance from User of django that has is_active boolean field
class UsuarioRM(Usuario):
servicos = models.ManyToManyField(Servico,related_name='servicos_usuario', blank=True)
# SIGNALS
from django.db.models import signals
from django.db.models.signals import m2m_changed
def usuariorm_servicos_changed(sender, **kwargs):
action = kwargs.pop('action', None)
pk_set = kwargs.pop('pk_set', None)
instance = kwargs.pop('instance', None)
if action == "pre_clear":
if instance.servicos.all():
servicos = instance.servicos.all()
for servico in servicos:
instance.servicos.remove(servico)
instance.save()
else:
instance.is_active = False
instance.save()
if action == "post_add":
if pk_set:
instance.is_active = True
else:
instance.is_active = False
instance.save()
m2m_changed.connect( usuariorm_servicos_changed, sender=UsuarioRM.servicos.through )