How to obtain the id of a self (onchange) odoo

Use the self._origin.id

vITraining Admin

If you have this code onchange:

@api.onchange('a_field') 
def _onchange_cor(self):
if self.a_field:
self.env.cr.execute('SELECT name from a_table where field_id=%s' , (self.id,))

but self.id returns <openerp.models.NewId object at 0x7f904b586e10>

How to get the id of self (the current record)?


In onchange, odoo creates a new object and replaces self with it. The origin object, what you are looking for can be found under self._origin.id