There are some customized singals in django that are used to listen for some actions and send out notifications
Official explanation:
Django provides a "signal distributor" that allows decoupled applications to be notified when actions occur elsewhere in the framework.
Simply put, signals allow a specific sender to notify a group of receivers that some action has occurred. This is useful in cases where multiple pieces of code are associated with the same event.
Some singals are already built into django, in django/db/models/, such as
Model signals pre_init # django's modal is automatically triggered before it executes its constructor method post_init # django's modal is automatically triggered after executing its constructor methods pre_save # django's modal objects are automatically triggered before saving the post_save # django's modal object is saved and automatically triggers the pre_delete # django's modal objects are automatically triggered before deleting the post_delete # django's modal object deletion automatically triggers the m2m_changed # django's modal automatically triggers before and after manipulating the third one (add,remove,clear) using the m2m field class_prepared # At program startup, detect modal classes in registered apps, and for each class, automatically trigger the Management signals pre_migrate # Automatically triggered before executing the migrate command post_migrate # Automatically triggered after execution of the migrate command Request/response signals request_started # Automatically triggered before the request arrives request_finished # Automatically triggered at the end of the request got_request_exception # Automatically triggered after a request exception Test signals setting_changed # Automatically triggered when modifying configuration files using the test test template_rendered # Automatically triggered when rendering templates using test test Database Wrappers connection_created # Automatically triggered when creating a database connection
Usage:
Using these singals you can realize some linkage operations in the model, for example, to change the log of the operator when updating the record through the model, you can use the post_save decorator directly in the place of the operation.
Or rewrite post_save to log the relevant information once and for all. Or log the request information when the request is made.
from import request_finished from import receiver @receiver(request_finished) def my_callback(sender, **kwargs): print("Request finished!")
How to customize singals?
a. Definition of the singal file
import pizza_done = (providing_args=["toppings", "size"])
b. Registration of singal
def callback(sender, **kwargs): print("callback") print(sender,kwargs) pizza_done.connect(callback)
c. Trigger signals
from trails import pizza_done pizza_done.send(sender='seven',toppings=123, size=456)
Demand Scenario:
There is a requirement in the project, when the model (i.e. library data) is modified or deleted, automatically trigger a redis synchronization task (later found that this requirement does not make sense ....). The model's save has post_save, delete has post_delete, only no update, and the code uses update in quite a lot of scenarios, so I searched why there is no update singals.
See:/ticket/12184
In fact, a long time ago, someone to django official mentioned this way, why not add in the official version, specifically why this pr has not been accepted, you can see the discussion inside, anyway, at that time, django1.9 still does not support, can only be used by their own first to write a use, there is a problem and then undo it well.
Solution:
file
# coding:utf-8 from import Signal post_update = Signal(providing_args=["user"])
file
----------- Override the update method in the queryset for a certain mod -----------
// Introduce a customized signal file from tools import signals class MyCustomQuerySet(): def update(self, **kwargs): super(MyCustomQuerySet, self).update(**kwargs) // Send the singalsignals when update is called. signals.post_update.send(sender=, user="xxx") print("finished!") class MyCustomManager(): def get_queryset(self): return MyCustomQuerySet(, using=self._db) class crontab_ping(): name = (max_length=64, blank=True, null=True) objects = MyCustomManager()
Documentation:
------- receives signal, triggering the operation ----------
from import post_update @receiver(post_update) def post_update_callback(sender, **kwargs): print(kwargs['user']) print("post_update_success")
This is the whole content of this article.