亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

利用django model save方法對未更改的字段依然進行了保存

瀏覽:13日期:2024-10-17 11:55:20

我就廢話不多說了,直接上代碼吧!

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 = 10obj.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_finishedrequest_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來發送信號)

利用django model 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這兩個模型信號的使用

利用django model save方法對未更改的字段依然進行了保存

和save的運行邏輯一樣,pre信號先觸發,post后觸發

以上這篇利用django model save方法對未更改的字段依然進行了保存就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 性生活黄色大片 | 鲁大师视频在线www观看 | 国产日韩精品欧美一区喷 | 欧美一区二区三区久久综合 | 任我鲁这里有精品视频在线播 | 麻豆国产入口在线观看免费 | 香蕉高清免费永久在线视频 | 女人被狂躁后的视频免费 | 成人国产在线24小时播放视频 | 成人在线视频网站 | 91精品乱码一区二区三区 | 国产成人综合久久精品红 | 亚洲欧美日韩综合在线播放 | 性做久久久久久久免费观看 | 美利坚永久精品视频在线观看 | 全免费毛片在线播放 | 国产永久在线 | 天堂黄网 | 国产免费久久精品久久久 | 亚洲免费色 | 欧美三级欧美一级 | 免费看精品黄线在线观看 | 999久久免费高清热精品 | 红色一级毛片 | 成人社区网站 | 国内自拍视频网站 | 香蕉在线视频网站 | 久久www免费人成精品 | 国产极品美女网站在线观看 | 亚洲第一区视频在线观看 | 欧美在线观看一区二区三区 | 国内真实愉拍系列情侣 | 亚洲精品系列 | 精品国产午夜久久久久九九 | 亚洲免费视频网址 | 国产裸舞福利在线视频合集 | 国产全黄a一级毛片视频 | 国产青青草视频 | 亚洲无线一二三四区手机 | 国产精品探花千人斩久久 | 青草视频免费观看在线观看 |