支持HW团队,就支付宝领取下面的红包吧! (打开支付宝就能领取!er1OEj73Uj), (打开支付宝收索“516503473”), 你领取消费,HW有奖励。红包使用无条件限制,有条件请注意是不是有病毒。

Login or Sign up | Validate
| Search

博主:初学MPEG

初学MPEG 本博客-采用Python的web框架Django与Mysql数据库,致力于对Python、Django的了解 与研究
Django技术QQ群:XXXXXXX
Python技术QQ群:XXXXXXX

Category

Keywords

本站最新博文

友情链接  

【转】Django Signals之pre_save & post_save ,pre_delete & post_delete

类别:其他 状态:游客可见,可回,会员可关联(没审核) 阅读:1303 评论:0 时间:Feb. 8, 2019, 2:29 p.m.
关键字:

 来源:https://blog.csdn.net/lockey23/article/details/80805758

Listening to signals(信号监听) 
To receive a signal, register a receiver function using the Signal.connect() method. The receiver function is called when the signal is sent.(为了接收信号,需要使用Signal.connect()方法注册一个接收器函数,当信号发送后接收器函数就会被调用)
Signal.connect(receiver, sender=None, weak=True, dispatch_uid=None)[source]
Parameters: 参数解析
receiver – The callback function which will be connected to this signal. See Receiver functions for more information.
sender – Specifies a particular sender to receive signals from. See Connecting to signals sent by specific senders for more information.
weak – Django stores signal handlers as weak references by default. Thus, if your receiver is a local function, it may be garbage collected. To prevent this, pass weak=False when you call the signal’s connect() method.
dispatch_uid – A unique identifier for a signal receiver in cases where duplicate signals may be sent. See Preventing duplicate signals for more information.

一般使用是这样的:

def my_callback(sender, **kwargs):
    print("Request finished!")


#Connecting receiver functions(连接接收器函数)
#There are two ways you can connect a receiver to a signal. You can take the manual connect route:

from django.core.signals import request_finished

request_finished.connect(my_callback)

当然,喜欢装逼(高技术)的人喜欢使用另外一种方式,receiver()装饰器:

receiver(signal)[source]#用法
Parameters: signal – A signal or a list of signals to connect a function to.

下面将针对本文标题来做示例:

django.db.models.signals.pre_save & django.db.models.signals.post_save

Sent before or after a model’s save() method is called. 在模型保存操作执行前或者执行后发送信号

Connecting to signals sent by specific senders(连接到特定发送器发送的信号)

Some signals get sent many times, but you’ll only be interested in receiving a certain subset of those signals. For example, consider the django.db.models.signals.pre_save signal sent before a model gets saved. Most of the time, you don’t need to know when any model gets saved – just when one specific model is saved.(有些信号会被多次发送,但是我们通常只是对其中的一些信号子集感兴趣,下面将演示针对具体的某个模型的pre_save以及post_save来发送信号)
这里写图片描述
从上边的运行结果可以看出,两个函数都被执行了,但是是有一定的执行顺序的,pre then post
In these cases, you can register to receive signals sent only by particular senders. In the case of django.db.models.signals.pre_save, the sender will be the model class being saved, so you can indicate that you only want signals sent by some model:
django.db.models.signals.pre_delete & django.db.models.signals.post_delete

Sent before or after a model’s delete() method or queryset’s delete() method is called. 在模型删除操作执行前或者执行后发送信号

  • 1
  • 2

下面将演示pre_delete与post_delete这两个模型信号的使用

这里写图片描述

 

和save的运行逻辑一样,pre信号先触发,post后触发

 

操作:

Please Login (or Sign Up) to leave a comment