Community mailing list archives

community@mail.odoo.com

Update field on another field change

by
Maxim Litnitskiy
- 10/15/2014 18:08:43
Hi dear Developers.
Can please anybody give a clue how to update a field when another field is changed.
I have a model with the following fields: state,  start_time, end_time.
I need to have an update trigger to record start_time / end_time on state change.

state = fields.Selection(SESSION_STATE_CHOICES, string=_('State'))
start_time = fields.Datetime(string=_('Started'))
end_time = fields.Datetime(string=_('Ended'))

>From the documentation:

 This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is altered by ORM or changed in the form:

    @api.multi
    @api.depends('state')
    def _on_state_change(self):
        _logger.debug('SESSION STATE CHANGE: %s' % self.state)
        for rec in self:
            if rec.state == 'running':
                rec.start_time = datetime.datetime.now()
            elif rec.state in ['done', 'cancelled']:
                rec.end_time = datetime.datetime.now()


_on_state_change is never called!


Tried to use field inverse attribute. Like this:
state = fields.Selection(SESSION_STATE_CHOICES, inverse='_on_state_change', string=_('State'))
Also does not work. _on_change_state is never called.

So. How to update one field when another one is changed?
Thanks.