This question has been flagged
4 Replies
20230 Views

I would like to use api.onchange as a trigger because I don't want to inherit base fields and use api.depends.
A simple example:

class pos_session2(models.Model):
_inherit = 'pos.session'

@api.one #with or without doesn't make any difference
@api.onchange('start_at','stop_at')
def _session_costs_control(self):
_logger.info('Id = %s', self.id)

I get no result (or errors) in log.

How should I call the method?

Edit: Temporary solved creating a  new check field on the same table and calling api.depends.

Avatar
Discard
Best Answer

Hi,

You dont need to call the method in new api, the method will automatically invokes when any change is acting on 'start_at','stop_at' these fields.

Avatar
Discard
Author

Problem is the method is not triggered.

In this specific example when I open a new pos session Odoo create the record for 'start_at'. When I close the session Odoo fill with the right datetime the field 'stop_at'.

In both cases _logger.info doesn't show nothing.

Is it possible that onchange is not triggered because it can't see a creation of a new field in a record?

Best Answer

did you change 'start_at' and 'stop_at' by hand in browser? it will be invoked only if you change them. 
Note: never use @api.one if you intend to return a value from method because it will wrap the returned value in a list. 

Avatar
Discard
Author

No, with the new API you don't need to edit the views. From the V10 Docs:

Both computed fields and new-API onchanges are automatically called by the client without having to add them in views.

It is possible to suppress the trigger from a specific field by adding on_change="0" in a view:

<field name="name" on_change="0"/>

will not trigger any interface update when the field is edited by the user, even if there are function fields or explicit onchange depending on that field.

About @api.one, I just tried with and without for a test. I prefer stay with api.depends for now.

you do not need to add on_change() in xml but you need a field in view. on_cahnge() is triggered at client side. so if there is no change in value on_change will not be called.

Author

Understood. Thanks.