Detailed explanation of the observer pattern in Django

Y2J
Release: 2017-05-13 13:47:00
Original
1769 people have browsed it

"Signal dispatch" is provided in Django for decoupling when the framework performs operations. In layman's terms, a signal allows a specific sender to alert some recipients when some action occurs.

Django provides a signal mechanism. In fact, it is the observer pattern, also called publish-subscribe (Publish/Subscribe). When some action occurs, a signal is emitted, and then the function that listens to the signal will be executed.

Django has some built-in signals, such as:


django.db.models.signals.pre_save 在某个Model保存之前调用
django.db.models.signals.post_save 在某个Model保存之后调用
django.db.models.signals.pre_delete 在某个Model删除之前调用
django.db.models.signals.post_delete 在某个Model删除之后调用
django.core.signals.request_started 在建立Http请求时发送
django.core.signals.request_finished 在关闭Http请求时发送
Copy after login

What we have to do is to register a receiver function. For example, if you want to print a line after each request is completed.

You can use the callback method to register:


# receiver
def my_callback(sender, **kwargs):
  print("Request finished!")
 
# connect
from django.core.signalsimport request_finished
 
request_finished.connect(my_callback)
Copy after login

You can also use the decorator method to register. The following code is completely equivalent to the above.


from django.core.signalsimport request_finished
from django.dispatchimport receiver
 
@receiver(request_finished)
def my_callback(sender, **kwargs):
  print("Request finished!")
Copy after login

receiverCallback functionIn addition to using sender, you can also use some other parameters, such as for the pre_save function:

sender: sender (if it is pre_save, it is model class)
instance: instance
raw
using
update_fields
post_save() is a more practical function that can support some linkagerenew. Instead of having us write it in the view every time. For example: If a user submits a refund application, we need to change the status of the order to the "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() # 这里,order是refundform的一个外键
Copy after login

Of course, you can write more and more comprehensive things here, such as canceling the refund order and changing its status back.

Observer is a very practical design pattern. Django also supports user-defined signals.

【Related Recommendations】

1. Special Recommendation: "php Programmer Toolbox" V0.1 version download

2. Python free video tutorial

3. Python object-oriented video tutorial

The above is the detailed content of Detailed explanation of the observer pattern in Django. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!