Python Road--web--2--django-11-Signal

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.