Hello, Odooers & Developers!
Welcome to this Odoo technical blog, where I'll share insights on using the @api.onchange method in Odoo effectively. The change method is a powerful tool that helps dynamically update field values in a form view based on user input. It improves the user experience by making changes without requiring a page refresh.
In this blog, we'll cover:
What the @api.onchange decorator is, and how it works?
How to use onchange to modify field values dynamically.
Practical examples to demonstrate its practical applications.
What is the @api.onchange Method?
The @api.onchange decorator in Odoo automatically updates field values when a specified field changes in the form view. It is primarily used in the frontend (client-side) and does not affect the database directly.
Key Features of Onchange method in odoo
1. Executes When a Field is Modified
- The procedure executes only if a user edits a particular field in the form view. It does not run when loading a record; it only reacts to changes.
2. Does Not Store Values in the Database
- Changes are visible in the UI but are only saved if the user explicitly saves the record. If the user discards the changes, the computed values are lost.
3. Works Only in Form Views
- The change method is implemented only in form views. It does not work in list (tree), kanban, or other view types.
4. Instant Field Updates Without Page Refresh
- When a related field changes, dependent fields update instantly without requiring a page refresh. This improves usability and provides a smoother user experience.
5. Can Modify Multiple Fields at Once
- You can update multiple fields dynamically based on changes in one or more fields.
Example: Changing a product in an invoice can automatically update the price, tax, and discount fields.
How to Use @api.onchange in Odoo
Basic Syntax:
from odoo import models, fields, API
.py
class ExampleModel(models.Model):
_name = 'example.model'
_description = 'Example Model'
price = fields.Float(string='Price')
tax = fields.Float(string='Tax')
total_price = fields.Float(string='Total Price', compute='_compute_total_price')
@api.on change('price', 'tax')
def _onchange_total_price(self):
if self. Price and self.tax:
self.total_price = self.price + (self.price * self.tax / 100)
else:
self.total_price = self.price
Explanation:
@api.onchange('price', 'tax') triggers _onchange_total_price when either price or tax is updated.
The method calculates the total price dynamically without saving the record.
Python Model (.py) Example:
from odoo import models, fields, API
.py
class LeaseContract(models.Model):
_name = 'lease.contract'
_description = 'Lease Contract'
shop_id = fields.Many2one('mall.shop', string='Shop')
rent_amount = fields.Float(string='Monthly Rent', readonly=True)
@api.on change('shop_id')
def _onchange_shop(self):
if self.shop_id:
self.rent_amount = self.shop_id.rent_amount
else:
self.rent_amount = 0.0
Explanation:
When a user selects a shop, the system automatically fills the following fields in the lease contract:
Monthly Rent – Auto-filled based on the shop's rent.
If no shop is selected, the fields are reset to empty or default values:
Monthly Rent → Set to 0.0
Benefits of Onchange Methods:
Instant Updates – When a user changes a field, other related fields update automatically, improving the experience.
Brilliant Forms – You can set up rules to show, hide, or modify fields without saving changes to the database.
Dynamic Interaction – Forms adjust based on user input, making data entry faster and more convenient
Conclusion:
The @api.onchange method is a powerful tool in Odoo for dynamically updating fields in real time without refreshing the page.
Improves user experience with instant field updates.
Reduces unnecessary database writes.
Works seamlessly in form views.
By using @api.onchange effectively, you can create more interactive and user-friendly Odoo applications! This topic is straightforward to implement in your Odoo projects.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
if you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.