溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么利用django model save方法對(duì)未更改的字段進(jìn)行保存

發(fā)布時(shí)間:2021-06-17 15:39:05 來源:億速云 閱讀:434 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下怎么利用django model save方法對(duì)未更改的字段進(jìn)行保存,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

直接上代碼吧!

 obj = Obj.objects.get(id=1)
print obj.name #此時(shí)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結(jié)果依然為'abc'。save()保存時(shí),雖然沒有更改其它字段,但依然會(huì)將內(nèi)存中的值,再次存入數(shù)據(jù)庫(kù),子函數(shù)和其它進(jìn)程更改的值會(huì)被覆蓋。

補(bǔ)充知識(shí):Django Signals之pre_save & post_save ,pre_delete & post_delete

Listening to signals(信號(hào)監(jiān)聽)

To receive a signal, register a receiver function using the Signal.connect() method. The receiver function is called when the signal is sent.

(為了接收信號(hào),需要使用Signal.connect()方法注冊(cè)一個(gè)接收器函數(shù),當(dāng)信號(hào)發(fā)送后接收器函數(shù)就會(huì)被調(diào)用)

Signal.connect(receiver, sender=None, weak=True, dispatch_uid=None)[source]

Parameters: 參數(shù)解析

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(連接接收器函數(shù))
#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)

當(dāng)然,喜歡裝逼(高技術(shù))的人喜歡使用另外一種方式,receiver()裝飾器:

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

下面將針對(duì)本文標(biāo)題來做示例:

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

Sent before or after a model's save() method is called.

在模型保存操作執(zhí)行前或者執(zhí)行后發(fā)送信號(hào)

Connecting to signals sent by specific senders

(連接到特定發(fā)送器發(fā)送的信號(hào))

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.

(有些信號(hào)會(huì)被多次發(fā)送,但是我們通常只是對(duì)其中的一些信號(hào)子集感興趣,下面將演示針對(duì)具體的某個(gè)模型的pre_save以及post_save來發(fā)送信號(hào))

怎么利用django model save方法對(duì)未更改的字段進(jìn)行保存

從上邊的運(yùn)行結(jié)果可以看出,兩個(gè)函數(shù)都被執(zhí)行了,但是是有一定的執(zhí)行順序的,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.

在模型刪除操作執(zhí)行前或者執(zhí)行后發(fā)送信號(hào)

下面將演示pre_delete與post_delete這兩個(gè)模型信號(hào)的使用

怎么利用django model save方法對(duì)未更改的字段進(jìn)行保存

和save的運(yùn)行邏輯一樣,pre信號(hào)先觸發(fā),post后觸發(fā)

以上是“怎么利用django model save方法對(duì)未更改的字段進(jìn)行保存”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI