odooopenerp-7

OpenERP 7 : How to trigger a auto version increase function after click save


how could i trigger a function after i click save in OpenERP 7?

In my custom module, i wish to auto increase the "version_number" parameter where every time the user click save it will trigger a function to execute the logic of "ver=ver+1" and write back to the "version_number" fields. How could i do that?

I tried use "def write()" but not sure exactly how it could be done. Appreciate your kind help. Thanks!


Solution

  • You need to override write method.

    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))