Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
21250 Vistas

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
Descartar
Mejor respuesta

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
Descartar
Autor

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?

Mejor respuesta

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
Descartar
Autor

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.

Autor

Understood. Thanks.

Publicaciones relacionadas Respuestas Vistas Actividad
1
sept 23
1834
2
feb 23
10317
1
feb 22
2393
0
mar 21
6121
0
sept 20
1843