您好,登錄后才能下訂單哦!
小編給大家分享一下怎么利用django model save方法對未更改的字段進行保存,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
直接上代碼吧!
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方法對未更改的字段進行保存”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。