The Django post_save() signal can be implemented by following these steps:
from django.db.models.signals import post_save
from myapp.models import MyModel
def my_signal_receiver(sender, instance, created, **kwargs):
if created:
print("A new object has been created:", instance)
else:
print("An object has been updated:", instance)
In this example, the function takes four arguments:
post_save.connect(my_signal_receiver, sender=MyModel)
This connects the function mysignalreceiver to the post_save signal for the MyModel model. Whenever a MyModel instance is saved or updated, the function will be called.
Note: You can connect multiple functions to the post_save signal for the same model.
Asked: 2022-08-07 11:00:00 +0000
Seen: 13 times
Last updated: Jun 18 '21