Enable Dark Mode!
By: Cybrosys Technologies

ORM Methods in Odoo 17

Technical Odoo 17

Odoo, a versatile business application framework, boasts a robust Object-Relational Mapping (ORM) layer that simplifies database operations. This layer facilitates seamless interaction with the database, allowing developers to effortlessly manipulate data and construct resilient applications. In this blog post, we will delve into the foundational concepts of Odoo's ORM methods and their pivotal role in ensuring efficient and streamlined database management.

The ORM methods in Odoo furnish a higher-level abstraction for interfacing with the database, negating the necessity for manually crafting intricate SQL queries. These methods encapsulate standard database operations, encompassing record creation, retrieval, updating, and deletion, offering a user-friendly interface for working with data. Among the frequently utilized ORM methods in Odoo are:

* create():  Model.create(vals_list)

This function is employed to generate a new entry in the database. It accepts a dictionary containing field-value pairs that represent the data intended for storage.

values = {
    'name': applicant.partner_name,
    'email': applicant.email_from,
    'phone': applicant.partner_phone,
    'mobile': applicant.partner_mobile,
    'degree': applicant.type_id.id,
    'employee_id': employee.id

The code creates a new record in the 'education.faculty' model in Odoo, populating it with values from the 'applicant' object such as name, email, phone, mobile, degree, and employee ID. The 'create' method of the Odoo ORM is utilized for this database operation.

* copy() :Model.copy(default = None)

The 'copy' method generates a new record that mirrors the exact content of the original record.

product_duplicate = product.copy({'name': 'Duplicate'})

The line ".copy({'name': 'Duplicate'})" invokes the copy method on the product record. The provided dictionary {'name': 'Duplicate'} specifies the field to be updated and its new value. In essence, this action creates a copy of the product record, assigning the 'name' field of the duplicate as 'Duplicate'.

* flush():Model.flush(fnames=None, records=None)

The flush method is employed to manage pending calculations and updates on records within a model. It proves valuable in ensuring that any outstanding modifications are accurately reflected in the database.

self.env['account.move.line'].flush_model(['account_id', 'journal_id'])

The line ".flush_model(['account_id', 'journal_id'])" demonstrates the application of the flush method on the account.move.line model. The specified list ['account_id', 'journal_id'] includes the field names for which pending calculations and updates are to be addressed.

* search(): 

This function is employed to locate records based on specified conditions, providing a list of record ids that meet the search criteria.

customer = self.env['res.partner'].search([('id', '=', rec.partner_id.id)])

The line ".search([...])" showcases the utilization of the search method on the res.partner model. It accepts a domain as its parameter, defining the search criteria. The provided domain [('id', '=', rec.partner_id.id)] is a list containing a single tuple that outlines the condition for the search, specifically seeking records where the 'id' field (representing the primary key) matches the id of rec.partner_id.

* browse(): 

This function is employed to fetch records from the database using their IDs, yielding a recordset that encapsulates the identified records.

partner = self.env['res.partner'].browse(partner_id)

The line ".browse(partner_id)" demonstrates the application of the browse method on the res.partner model. The parameter 'partner_id' signifies the ID of the particular record intended for retrieval.

* read(): 

This function is employed to fetch the values of fields from one or more records. It requires a list of record IDs and a list of field names to be read.


The code employs the browse method to fetch a record from the res.partner model using the provided ID (self.partner.id). Subsequently, the read method is invoked on the retrieved record, reading the default fields defined for that record without requiring any additional arguments.

* write():

This function is utilized to modify the values of fields for one or more records. It accepts a dictionary containing field-value pairs that specify the updates to be applied.

self.write({'state': done})

The code ".write({'state': 'done'})" represents the execution of the write method on the current record(s). The provided dictionary {'state': 'done'} indicates the field to be updated ('state') and its new value ('done'), effectively modifying the state of the record(s).

* unlink(): 

This function is employed to remove one or more records from the database.


The code ".browse(self.group_private.id)" utilizes the browse method to fetch a particular record from the mail.channel model using the provided group_private.id. Accessing the ID of the record is done through self.group_private.id. Following this, the unlink method is invoked on the obtained record, and since unlink doesn't need any arguments, it promptly removes the record from the database.

* Map : Model.mapped(func):

The mapped method iterates through each record in the source_recordset, applies the specified func function, and gathers the outcomes in a fresh recordset called result_recordset.

invoices = self.mapped('invoice_ids')

* Sort : Model.sorted(key=None, reverse=False)

 Returns a self-ordered Recordset with the key.

      self.line_ids = self.line_ids.sorted(key=lambda x: x.product_id.name)

The sorted method is applied to arrange the records in the 'line_ids' recordset based on the 'product_id.name' field in ascending order. The lambda function serves as the sorting key.

Benefits of ORM Methods in Odoo

Streamlined Database Tasks: ORM methods offer a straightforward and user-friendly method for executing routine database operations, eliminating the need for intricate SQL queries.

Cross-Platform Adaptability: Utilizing ORM methods enhances code portability and compatibility across various database backends.

Improved Code Clarity and Manageability: ORM methods contribute to code clarity and ease of maintenance by offering a uniform and standardized means of engaging with the database.

Safeguarding Data Integrity and Security: In Odoo ORM layer plays a crucial role in preserving data integrity and security through transaction management, enforcement of referential integrity, and implementation of access control mechanisms. If you want to know more about ORM methods in Odoo, Please refer to our previous blog.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




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



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



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

Send Us A Message