Defining onchange with the compute method
In the last two recipes, we saw how to define and call the onchange
method. We also saw its limitation, which is that it can only be invoked automatically from the user interface. As a solution to this problem, Odoo v13 introduced a new way to define onchange
behavior. In this recipe, we will learn how to use the compute
method to produce behavior similar to that of the onchange
method.
Getting ready
For this recipe, we will use the my_hostel
module from the previous recipe. We will replace the onchange
method of hostel.student
with the compute
method.
How to do it...
Follow these steps to modify the onchange
method with the compute
method:
- Replace
api.onchange
in theonchange_duration()
method withdepends
, like this:@api.depends('admission_date', 'discharge_date') def onchange_duration(self): ...
- Add the
compute
parameter in the definition of the field...