ORM Methods and Decorators in Odoo16 | Odoo ORM Methods

December 20, 2022

On change stands for Object Relational Mapping. With the help of ORM methods, it reduces the gap between Open-objects and SQL tables. Object Relational Mapping (ORM) helps to manage the creation of tables and fields in Odoo. In Odoo, ORM methods help to create updates, delete, search, etc in the database.

Common ORM Methods in Odoo

    1. create()

The create () method is used to create records in the database. First, specify the model and define a dictionary.

Example: In the below Eg hr. employee is model and name is filed

self.env [‘hr. employee’].create({‘name’:”Employee         One”})


The write() method is used to update or write the values of the record set of your Database. Specify the data dictionary and the record is updated with these values.

Eg: In the below example update the value of the state field to create

       self.write({“state”: “create”})


 This method is used to search records within the model based on some condition. In this method pass arguments.

E.g.: In the below example, search records from the hr. Employee table with the domain name.


It returns the employee records named ABC
  1. search_count()

 This method returns the number of records in the search method.



In the above e.g. the methods return the total number of Employees named ABC

 Request Your Free Quote

5. browse()

 This method is used for a record set with an ID

Example :


In the above eg you have specified the ID of the record.

6. copy()

The copy() method is used to duplicate a data record in the current model.


  1. Unlink()

The unlink() method is used to delete the data record from the database.



 In this above example, the active record becomes deleted

8. ensure_one()

The ensure_one() method checks whether the record is single or not

If not single raises an error.



  In the above eg check in the current record is single or not

  1. filtered()

  This method is used to filter records with specific conditions in the database

eg :
 self.env[‘hr.employee’].filtered(lambda x:x.name ==      “ABC”)

 In the above eg returns list of an employee named ABC

  1. mapped()

 This mapped() method returns the field values

Example :


The above example returns the country of an employee whose name is “ABC”.

  1. sorted()

 This sorted() method is used to sort records into a specific condition

Eg: self.env[‘hr.employee’].sorted(key=lambda r: r.name)

 In the above example sort employees by their name

Common Decorators in Odoo

1 . API.depends

eg  :


def _compute_status(self):

for record in self:

record.status = record.state

In the above example, status is a computer function and it depends on the field ‘state’, If any changes in state the value of status becomes a change

2 . api.change

eg  :


def _onchange_status(self):

for record in self:

record.status = record.state

This decorator api.onchange is used when there is a need to change a field’s value according to one field. In the above example value of status automatically changed.


          eg :


          def _onchange_status(self):

for record in self:

if record.status != record.state:

                                      raise UserError(_(“Invalid ......”))

This method is used to raise an exception if its condition is not satisfied

  1. API.model

This decorator will convert old API calls to the decorated functions to a new API signature


          def create(self,vals):

                   res = super().create(vals)


                   return res


"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *