Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: cannot detect changes on many-to-many field with m2m_changed signal - auditing at model-level

I'd like to keep track on what field has changed on any model (i.e. audit at model level since it's more atomic, not at admin/form-level like what django and django-reversion can already do). I'm able to do that for any field using pre/post save/delete signals. However, I have a problem of doing that on an m2m field.

For the code sample below, i define 'custom_groups' m2m field in user change form since it's a reverse relation. When user saves the form on admin interface for example, I'd like to log if there's a change in 'custom_groups' field.

Model:

from django.contrib.auth.models import User

class CustomGroup(models.Model):
    users = models.ManyToManyField(User, related_name='custom_groups')

ModelForm:

class CustomUserChangeForm(UserChangeForm):
    custom_groups = forms.ModelMultipleChoiceField(required=False, queryset=CustomGroup.objects.all())

The problem with using m2m_changed signal is that i can't check what has actually changed for the case where the m2m field is updated using assignment operator:

user.custom_groups = self.cleaned_data['custom_groups']

This is because internally django will perform a clear() on *custom_groups*, before manually adding all objects. This will execute pre/post-clear and then pre/post save on the m2m field.

Am I doing all this the wrong way? Is there a simpler method that can actually work?

Thanks!

like image 891
Edwin Avatar asked Jan 20 '11 08:01

Edwin


1 Answers

I had a similar problem and I think I could solve it. I don't know how you are using the m2m_changed but it should be on models.py and should be similar to something like this:

signals.m2m_changed.connect(your_function, sender=CustomGroup.users.through)

Now, I would create a signals.py file containing that function, and the following code should print you the options that you have selected:

def your_function(sender, instance, action, reverse, model, pk_set, **kwargs):
    if action == 'post_add':
        for val in pk_set:
            print val

Now, you know the updated values. I hope this could solve your problem.

like image 200
Kiko Fernandez Avatar answered Jan 04 '23 12:01

Kiko Fernandez