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 )
m2m_changed. Sent when a ManyToManyField is changed on a model instance. Strictly speaking, this is not a model signal since it is sent by the ManyToManyField , but since it complements the pre_save / post_save and pre_delete / post_delete when it comes to tracking changes to models, it is included here.
Django Signals - post_delete() To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.
pre_save) is provoked just before the model save() method is called, or you could say model save method is called only after pre_save is called and done its job free of errors.
There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With