为了检查字段是否已更改,我在模型中添加了一个属性,如下所述:
class Person(models.Model):
name = models.CharField()
__original_name = None
def __init__(self, *args, **kwargs):
super(Person, self).__init__(*args, **kwargs)
self.__original_name = self.name
def save(self, force_insert=False, force_update=False, *args, **kwargs):
if self.name != self.__original_name:
# name changed - do something here
super(Person, self).save(force_insert, force_update, *args, **kwargs)
self.__original_name = self.name
我已经有这个模型的实例(没有 __original_name 属性),当我想修改并保存一个时,我得到
'Person' object has no attribute '__original_name'
有人可以帮忙吗?