As a new Odoo developer, one of the most important things to understand at the outset, as you begin your learning journey, is how data updates in Odoo. Currently, Odoo uses an Object Relational Mapping system, which enables you to access database elements using simple Python code instead of complex SQL queries.
The most widely used ORM techniques include the use of a write() method. The main role of this method is to update existing records in Odoo. Every time you edit a record in the Odoo interface—such as changing a product price, updating a customer address, or modifying an order status—the write() method is called.
This blog is designed for beginners and other Odoo developers who are new to the framework. We will try to explain the write method in very simple words with step-by-step explanations along with examples so you can confidently use it in your own custom modules.
What is the write() method in Odoo?
The write() method is an ORM method used to update one or more existing records in a model.
In simple terms:
- create() > creates new records
- write() > updates existing records
- unlink() > deletes records
Whenever you edit a record in the Odoo UI and click Save, Odoo internally calls the write() method.
Basic Syntax of write()
recordset.write(values)
Where:
- recordset > one or more records of a model
- values > a dictionary containing field names and their new values
Example
partner = self.env['res.partner'].browse(10)
partner.write({'name': 'New Customer Name'})
This updates the name of the partner whose ID is 10.
Writing on Multiple Records
One of the strengths of Odoo ORM is that write() works on recordsets, not just single records.
Example
partners = self.env['res.partner'].search([('is_company', '=', True)])
partners.write({'comment': 'This is a company'})This updates all company partners in a single operation. Odoo automatically loops internally, so you don’t need a Python for loop.
Overriding the write() Method
Often, you may want to add custom logic when a record is updated. In such cases, you can override the write() method.
Basic Override Example
class ProductTemplate(models.Model):
_inherit = 'product.template'
def write(self, vals):
res = super().write(vals)
# Custom logic here
for record in self:
record.message_post(body="Product record was updated.")
return res
Important Points
- Always call super() to ensure Odoo’s default behavior works correctly
- Always return the result of super() (usually True)
Practical Example: Tracking Price Changes
Let’s say you want to log a message whenever a product’s price is updated.
class ProductTemplate(models.Model):
_inherit = 'product.template'
def write(self, vals):
if 'list_price' in vals:
for product in self:
old_price = product.list_price
new_price = vals['list_price']
product.message_post(
body=f"Price will change from {old_price} to {new_price}"
)
return super().write(vals)
What’s Happening Here?
- We check if list_price is being updated
- We loop through the recordset
- We post a message in the chatter
- Finally, we call super().write()
Difference Between write() and update()
In Odoo:
- write() > is the standard ORM method used to update records in the database.
- update() > usually refers to Python dictionary update or internal cache methods
Always prefer write() unless you have a very specific reason.
Using write() in Button Actions
You can use write() inside button methods.
def action_confirm(self):
self.write({'state': 'confirmed'})
This updates the state field for all selected records.
Understanding sudo() in Odoo write() Operations
When working with the write() method, beginners often encounter access rights errors.
This usually happens when the current user does not have permission to update a record. In such cases, Odoo provides a special method called sudo().
What is sudo() in Odoo?
sudo() temporarily executes an operation with superuser privileges, bypassing normal access rights and record rules.
Example:
self.sudo().write({'state': 'confirmed'})Use sudo() in situations like scheduled actions, integrations, or automated processes where normal user permissions may not be enough.
Important: Avoid using sudo() unnecessarily, as it bypasses security rules and can allow unintended data changes. Always use it only when system-level updates are required.
Common Mistakes to Avoid
def write(self, vals):
# Wrong: breaks default behavior
pass
- Modifying self Before super()
Always remember: values in self may not yet reflect vals.
- Using write() Inside a Loop Unnecessarily
for rec in records:
rec.write({'active': False})
Better:
records.write({'active': False})Performance and Best Practices
- Use write() on recordsets instead of loops
- Keep custom logic lightweight
- Avoid heavy computations inside write()
- Use @api.constrains or @api.onchange when appropriate
- Always return the result of super()
The write() ORM method is a core concept every Odoo developer must understand. It is the standard and safe way to update records while respecting Odoo’s business logic, access rules, and data integrity.
For beginners, mastering write() will help you:
- Customize Odoo behavior
- Add validations and automation
- Build clean and maintainable modules
As you continue learning Odoo 19, you will see write() used everywhere—from simple field updates to complex business workflows. Understanding it early will make your Odoo development journey much smoother.
To read more about How to Create() orm method in Odoo 19, refer to our blog How to Create() orm method in Odoo 19.