利用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 = 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來發送信號)
從上邊的運行結果可以看出,兩個函數都被執行了,但是是有一定的執行順序的,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方法對未更改的字段依然進行了保存就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: