我就废话不多说了,直接上代码吧!
obj = Obj.objects.get(id=1) print obj.name #此时name的值假定为'abc' def handler(oid): obj = Obj.objects.get(id=oid) obj.name = '123' obj.save() handler(obj.id) obj.age = 10 obj.save() print obj.name
最终的name结果依然为'abc'。save()保存时,虽然没有更改其它字段,但依然会将内存中的值,再次存入数据库,子函数和其它进程更改的值会被覆盖。
补充知识:Django Signals之pre_save & post_save ,pre_delete & post_delete
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.
在模型删除操作执行前或者执行后发送信号
下面将演示pre_delete与post_delete这两个模型信号的使用
和save的运行逻辑一样,pre信号先触发,post后触发
以上这篇利用django model save方法对未更改的字段依然进行了保存就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 好薇2024《兵哥哥》1:124K黄金母盘[WAV+CUE]
- 胡歌.2006-珍惜(EP)【步升大风】【FLAC分轨】
- 洪荣宏.2014-拼乎自己看【华特】【WAV+CUE】
- 伊能静.1999-从脆弱到勇敢1987-1996精选2CD【华纳】【WAV+CUE】
- 刘亮鹭《汽车DJ玩主》[WAV+CUE][1.1G]
- 张杰《最接近天堂的地方》天娱传媒[WAV+CUE][1.1G]
- 群星《2022年度发烧天碟》无损黑胶碟 2CD[WAV+CUE][1.4G]
- 罗文1983-罗文甄妮-射雕英雄传(纯银AMCD)[WAV+CUE]
- 群星《亚洲故事香港纯弦》雨果UPMAGCD2024[低速原抓WAV+CUE]
- 群星《经典咏流传》限量1:1母盘直刻[低速原抓WAV+CUE]
- 庾澄庆1993《老实情歌》福茂唱片[WAV+CUE][1G]
- 许巍《在别处》美卡首版[WAV+CUE][1G]
- 林子祥《单手拍掌》华纳香港版[WAV+CUE][1G]
- 郑秀文.1997-我们的主题曲【华纳】【WAV+CUE】
- 群星.2001-生命因爱动听电影原创音乐AVCD【MEDIA】【WAV+CUE】