Nine, Signal
Django provides a "signal dispatch" for decoupling when the framework performs operations. In layman's terms, when something happens, the signal allows a specific sender to remind some of the recipients.
1. Django Built-in signal
1 Model Signals2Pre_init#The Django modal automatically fires before executing its construction method.3Post_init#when Django's modal executes its construction method, it automatically triggers4Pre_save#The Django Modal object is automatically triggered before it is saved5Post_save#when the Django modal object is saved, it is automatically triggered6Pre_delete#automatically triggers a Django modal object before it is deleted7Post_delete#automatically triggers when a Django modal object is deleted8M2m_changed#automatically triggers before and after the third table (Add,remove,clear) in the Django modal using the auto-Work field9Class_prepared#when the program starts, detects registered apps in the modal class, for each class, automatically triggersTen Management Signals OnePre_migrate#automatic triggering before executing the migrate command APost_migrate#automatically triggers after executing the migrate command -request/Response Signals -request_started#Auto-Trigger before request arrives theRequest_finished#automatically triggered when the request is complete -Got_request_exception#automatic triggering after an exception is requested - Test Signals -Setting_changed#automatically triggers when a configuration file is modified using test tests +Template_rendered#automatically triggers when a render template is tested with test - Database Wrappers +connection_created#automatically triggers when a database connection is created
For a Django built-in signal, you only need to register the specified signal and automatically trigger the registration function when the program performs the appropriate action:
View CodeView Code
2. Custom Signal
A. Defining the signal
1 Import Django.dispatch 2 pizza_done = django.dispatch.Signal (providing_args=["toppings"" size "])
B. Sign-up signal
1 def Callback (sender, * *Kwargs):2 print("callback" )3 print(sender,kwargs)4 5 Pizza _done.connect (callback)
C. Trigger Signal
1 from Import Pizza_done 2 3 pizza_done.send (sender='seven', toppings=123, size=456)
Since the trigger for the built-in signal is already integrated into Django, it is automatically called, and for custom signals it is necessary for the developer to trigger at any point.
MORE: Bash here
Python Road--web--2--django-11-Signal