Odoo 17 Development Book- Onchange with Compute Method

Onchange with Compute Method

Onchange and compute logic are two of Odoo's most commonly used techniques for achieving some level of business logic. A computed field is one that uses values from other fields to calculate its value. This method is used to compute the value of a computed field and is known as a compute method; onchange methods are methods that are triggered by a change in the field value.

Let's take a look at both scenarios with an example. Consider developing a rental order management business model. The duration and price for the unit duration are used to calculate the total rent. Let us define a monetary field total_rent in the following code as a computed field, which means that its value is determined by using the values of one or more other fields.


from odoo import models, fields
    class VehicleRental(models.Model):
       _name = "vehicle.rental"
       _description = "Vehicle Rental"
       hour_rate = fields.Monetary(string="Hour Rate",)
       hours = fields.Integer(string="Hours")
       total_rent = fields.Monetary(string='Total Rent',
                                    compute='_compute_total_rent')

       def _compute_total_rent(self):
           for record in self:
               record.total_rent = record.hour_rate*record.hours
                             

The value of computed fields is frequently reliant on other fields. As a result, it is advisable to include the dependencies in the compute method. The ORM anticipates that the developer will disclose these dependencies using the method's decorator depends(). When a change occurs in the dependent fields, ORM uses the specified dependencies to activate the compute method.


from odoo import models, fields, api
    class VehicleRental(models.Model):
       _name = "vehicle.rental"
       _description = "Vehicle Rental"
       hour_rate = fields.Monetary(string="Hour Rate",)
       hours = fields.Integer(string="Hours")
       total_rent = fields.Monetary(string='Total Rent',
                                    compute='_compute_total_rent')

       @api.depends('hour_rate', 'hours')
       def _compute_total_rent(self):
           for record in self:
               record.total_rent = record.hour_rate*record.hours

                             

With Odoo's Onchange mechanism, a field's value can be altered or updated if the values of other fields are changed. Furthermore, the onchange is only active in the form view and is triggered if one of the given fields in that view is edited. In the given example, the Reference for the rental order is achieved from a method. When the value of the form's vehicle_id field changes, the onchange() decorator causes the feature to invoke the onchange function.


from odoo import models, fields, api
    class VehicleRental(models.Model):
       _name = "vehicle.rental"
       _description = "Vehicle Rental"
       name = fields.Char(string="Ref.")
       vehicle_id = fields.Many2one('fleet.vehicle', string="Vehicle")

       @api.onchange('vehicle_id')
       def _onchange_vehicle(self):
           self.name = "Rental Order for %s" % self.vehicle_id.name
                             
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message