17

I am not very familiar with Django's signals and could use some help.

How do I modified the pk_set before the instance is saved? Do I have to return something to the signal caller (like the kwargs)? Or do I save the instance myself?

As a simplified example: I wanted to ensure the Category with pk=1 is included with all my Videos when they are saved. How would I do that with m2m_changed?

class Video(models.Model):
    category = models.ManyToManyField('Category')

def video_category_changed(sender, **kwargs):
    action = kwargs.pop('action', None)
    pk_set = kwargs.pop('pk_set', None)
    instance = kwargs.pop('instance', None)

    if action == "pre_add":
        if 1 not in pk_set:
            pk_set.update( [ 1 ] )  # adding this to the set
            # do something else?
            # profit?

m2m_changed.connect( video_category_changed, sender=Video.category.through )
thornomad
  • 6,707
  • 10
  • 53
  • 78
  • 1
    It's not required to return values in signals. You also don't need to save the instance. Just add the code to be executed on response to the selected events. – mvillaress Oct 24 '14 at 12:41

1 Answers1

49

Just updating the pk_set is sufficient. You don't need to do any extra work. Once the video instance is saved, it will have a category with pk=1.

from django.db import models
from django.db.models.signals import m2m_changed
from django.dispatch import receiver

class Category(models.Model):
    pass

class Video(models.Model):
    category = models.ManyToManyField('Category')

@receiver(m2m_changed, sender=Video.category.through)
def video_category_changed(sender, **kwargs):
    action = kwargs.pop('action', None)
    pk_set = kwargs.pop('pk_set', None)    
    if action == "pre_add":
        if 1 not in pk_set:
            pk_set.update([1])

In the above method, the categories will be saved only after the video instance is saved. If you want to EXPLICITLY save them in the m2m_changed instance, you can also do that as follows.

@receiver(m2m_changed, sender=Video.category.through)
def video_category_changed(sender, **kwargs):
    instance = kwargs.pop('instance', None)
    pk_set = kwargs.pop('pk_set', None)
    action = kwargs.pop('action', None)
    if action == "pre_add":
        if 1 not in pk_set:
            c = Category.objects.get(pk=1)
            instance.category.add(c)
            instance.save()
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
  • 2
    It's funny how little things stump you sometimes -- I didn't have a `pk=1` category object when I was testing this ... so, when it didn't work I assumed I was missing something with the signal since it's the first time I'd worked with those ... when in fact, I was just being silly and had it right to begin with. Thanks for the very clear answer with example. Nailed it. – thornomad Oct 24 '14 at 16:28