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 )