2

Lets say we have an object myobj which has fields f1, f2. Initially the fields are set to 1 and 2 and saved having pk=1

Now, I am calling it again something like this:

myobj.f1 = 11
myobj.f2 = 22
myobj.save()

while the model method is save(self, *args, **kwargs) I know, we can pass our own variables to it, override the method, do what ever we want..

Is there anyway that we can know previous data of the object? using some built-in arguments?

4

1 回答 1

3

没有任何内置方法可以在方法中获取旧数据save()。您将不得不查询数据库。像这样的东西:

class MyModel(Model):
   ...
   def save(self, *args, **kwargs):
       if self.pk:
          old_obj = MyModel.objects.get(pk=self.pk)

       #use old_obj for something

       ...
于 2013-08-27T06:11:30.213 回答