In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what is the signal mechanism of Django". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the signal mechanism of Django"?
Django provides a signaling mechanism. It is actually the Observer pattern, also known as publish-subscribe (Publish/Subscribe). When something happens, send a signal, and then the function that listens for the signal will be executed.
Django has some built-in signals, such as:
Django.db.models.signals.pre_save is called before a Model is saved
Django.db.models.signals.post_save is called after a Model has been saved
Django.db.models.signals.pre_delete is called before a Model is deleted
Django.db.models.signals.post_delete is called after a Model has been deleted
Django.core.signals.request_started sends when a Http request is made
Django.core.signals.request_finished sends when the Http request is closed
All we have to do is register a receiver function. For example, if you want to print a line of words after each request is completed.
You can use callback to register:
# receiverdef my_callback (sender, * * kwargs): print ("Request finished!") # connectfrom django.core.signalsimport request_finished request_finished.connect (my_callback)
You can also register as a decorator, and the following code is exactly equivalent to the above.
From django.core.signalsimport request_finishedfrom django.dispatchimport receiver @ receiver (request_finished) def my_callback (sender, * * kwargs): print ("Request finished!")
Besides using sender, the receiver callback function can also use some other parameters, such as for the pre_save function:
Sender: sender (model class if it is pre_save)
Instance: instance
Raw
Using
Update_fields
Post_save () is a more practical function that can support some linked updates. We don't have to write it in view every time. For example, if a user has submitted a refund request, we need to change the status of the order to "refunded" status. You can use the signaling mechanism without having to modify it everywhere.
@ receiver (post_save, sender=RefundForm) deforder_state_update (sender, instance, created, * * kwargs): instance.order.state = REFUNDING instance.order.save () # here, order is a foreign key of refundform
Of course, you can write more and more carefully here, such as canceling the refund slip and changing it back to the status.
Observer is a very useful design pattern, and Django also allows users to customize some signals.
At this point, I believe you have a deeper understanding of "what is the signal mechanism of Django". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.