Computed, Inverse, and Onchange functions are powerful tools in Odoo that help create dynamic and interactive business applications. These functions allow developers to automate calculations, manage data consistency, and enhance user experience within forms. Computed fields dynamically calculate values based on dependencies, while inverse methods enable users to update these fields manually. Onchange functions in Odoo respond instantly to changes in form fields without saving data. Together, they form the backbone of reactive behavior in Odoo modules.
Computed, Inverse and Onchange Functions in Odoo
When building custom modules in Odoo, you’ll often need to create dynamic fields that automatically calculate values, or update other fields when something changes. Odoo provides powerful tools like @api.depends, @api.onchange, and inverse methods to handle this behavior cleanly.
In this blog, we’ll walk you through how to implement these features using a practical use case, managing employee salary details with automatic calculations and dynamic form behavior.
Use Case: Employee Salary Logic
We want to manage employee salary information with the following logic:
- When a monthly salary is entered, the yearly salary should be automatically calculated.
- If a user edits the yearly salary, the monthly salary should update too.
- When the user selects an employee type (e.g., ‘Intern’, ‘Full-time’), the monthly salary should default to a preset value.
Let’s break this down step-by-step.
Compute Field in Odoo
A compute field in Odoo is a field whose value is calculated dynamically based on other fields rather than stored directly in the database (unless you specify store=True). store=True : Caches the value in the database for faster access.
This tells Odoo to cache the computed value in the database, which improves performance and allows the field to be used in search filters and views.
Step 1: Python Model (models/employee_salary.py)
from odoo import models, fields, api
from odoo.exceptions import UserError
class EmployeeSalary(models.Model):
_name = 'employee.salary'
_description = 'Employee Salary Info'
name = fields.Char(string="Employee Name", required=True)
employee_type = fields.Selection([
('intern', 'Intern'),
('fulltime', 'Full-time'),
('contract', 'Contract')
], string="Employee Type")
monthly_salary = fields.Float(string="Monthly Salary")
yearly_salary = fields.Float(
string="Yearly Salary",
compute="_compute_yearly_salary",
inverse="_inverse_yearly_salary",
store=True
)
@api.depends('monthly_salary')
def _compute_yearly_salary(self):
for rec in self:
rec.yearly_salary = rec.monthly_salary * 12
def _inverse_yearly_salary(self):
for rec in self:
rec.monthly_salary = rec.yearly_salary / 12
@api.onchange('employee_type')
def _onchange_employee_type_set_salary(self):
if self.employee_type == 'intern':
self.monthly_salary = 10000
elif self.employee_type == 'fulltime':
self.monthly_salary = 40000
elif self.employee_type == 'contract':
self.monthly_salary = 25000
Step 2: XML View
Here’s the corresponding XML file to display the form and list views of the salary model.
employee.salary.form
employee.salary
employee.salary.tree
employee.salary
Employee Salaries
employee.salary
list,form
Understanding the Decorators
@api.depends:
This tells Odoo that the yearly_salary depends on the value of monthly_salary. So when monthly_salary changes, Odoo recalculates yearly_salary.
Inverse Method in Odoo
Odoo computed fields are usually read-only. But if you want users to edit a computed field manually, you must define an inverse method. This lets Odoo know how to reverse-calculate the dependent field.
In our example:
Editing yearly_salary will update monthly_salary via the _inverse_yearly_salary method.
@api.onchange:
This is used to update field values dynamically in the form view, before saving. Here, we use it to set a default salary based on the selected employee type.
In our case:
When a user selects an employee_type, the system automatically sets a default monthly salary based on the type.
Final Output
Now, when you go to your custom model:
- Choosing ‘Full-time’ sets the monthly salary to ₹40,000 automatically.
- Changing the monthly salary auto-calculates the yearly salary instantly.
- Editing the yearly salary will update the monthly salary.
Using real ORM techniques, you’ve now built a smart, reactive Odoo form!
This demonstrates the power of Odoo’s ORM and decorators to create reactive, smart forms that simplify data entry and reduce human error.
Why These Features Matter
Computed fields, inverse methods, and onchange decorators are essential for every Odoo developer’s toolkit. With them, you can build responsive, user-friendly models that handle data elegantly and reduce errors. By understanding these features, you can:
- Automate complex calculations.
- Ensure data consistency across related fields.
- Improve user experience with dynamic, real-time updates.
If you’re building Odoo applications, integrating these decorators into your models will significantly enhance backend efficiency and usability.



