在更新之前获取字符串字段修改

Get the string field modification before it's update

我的 class reclamation.I 中有一个名为 'sujet' 的字符串字段,我一直在尝试在更新之前获取该字段值通过比较更新前后的字段值从我的字段中添加或删除

Exemple_1:点击保存按钮前的现场 Sujet http://i.stack.imgur.com/jDm3y.png

Exemple_2:点击保存按钮后的 Field Sujet http://i.stack.imgur.com/W74zA.png

输出:对我的字段的更改是:添加了 "is the best community"

为此,我立即想到像下面这样重写 write() 方法:

def write(self, cr, uid, ids, values, context=None):
        _logger.error("*******************OnUpdate***********************")
        #code before update print field sujet value
        _logger.error("Before:%s",values['sujet'])
        res = super(reclamation, self).write(cr, uid, ids, values, context=context)
        #code after update print field sujet value
        _logger.error("Before:%s",values['sujet'])
        return res

我面临的主要问题是,当我打印更新前的值时,我总是拥有更新后的值

问题解决了我做错了。 为了更好地理解解决方案,我们需要了解 write() 方法值 paramater.Values 在我的例子中包含新字符串 "Whosebugs community is the best community"。为了获取字段的古老值,我们显然需要在更新查询之前查询数据库。

def write(self, cr, uid, ids, values, context=None):
    _logger.error("*******************OnUpdate***********************")
    obj = self.browse(cr, uid, ids, context=context)
    _logger.error("Before:%s",obj.sujet)
    res = super(reclamation, self).write(cr, uid, ids,values,context=context)        
    _logger.error("After:%s",values['sujet'])
    return res

希望该解决方案对以后的人有所帮助,因为他们say.Read 文档