OpenERP 7:单击保存后如何触发自动版本增加功能

时间:2016-07-30 15:14:38

标签: openerp openerp-7

我在OpenERP 7中单击保存后如何触发功能?

在我的自定义模块中,我希望自动增加" version_number"参数,每次用户点击保存它将触发一个函数来执行" ver = ver + 1"并写回" version_number"领域。我怎么能这样做?

我尝试使用" def write()"但不确定它究竟是如何完成的。感谢您的帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

您需要覆盖write方法。

def write(self, cr, uid, ids, vals, context=None):
    res = super(your_model, self).write(cr, uid, ids, vals, context)
    self._increment_version(cr, uid, ids)
    return res

def _increment_version(self, cr, uid, ids):
    for record in self.browse(cr, uid, ids):
        cr.execute('update table_name set version_number=%s where id=%s' % (record.version_number + 1, record.id))

答案 1 :(得分:1)

你是对的,你应该使用写函数。

_columns={...,
      'ver':fields.integer(),
    ...}

def write(self, cr, uid, ids, vals, context=None)
    vals['ver']= trigger_func()
    return self.super(your_class_name).write(cr, uid, vals, context)   

答案 2 :(得分:1)

以下write方法适用于我

def write(self, cr, uid, ids, vals, context=None):
    res = super(your_model, self).write(cr, uid, ids, vals, context)
    self._increment_version(cr, uid, ids)
    return res

def _increment_version(self, cr, uid, ids):
    res = {}
    reads = self.read(cr, uid, ids, ['version'], context=context)
    ver_num = reads.get('version')
    ver_id = reads.get('id')
    if ver_num:        
        cr.execute('update table_name set version_number=%s where id=%s' % (ver_num + 1, ver_id))
    return res