Enable Dark Mode!
what-are-orm-methods-in-odoo-18.jpg
By: Athira K

What are ORM Methods in Odoo 18

Technical Odoo 17

Introduction

In Odoo, ORM (Object-Relational Mapping) methods are the core of interacting with the database, allowing developers to create, read, update, and delete records from the models easily. These ORM methods abstract the complexity of SQL queries, enabling developers to focus on business logic rather than database management.

Basic ORM Methods in Odoo

1. Create:

* Used to create new records in the database.

* Model.create(vals_list)

* Accepts a dictionary of values in a list.

* It returns a recordset (a list of new records created).

new_record = self.env['model.name'].create({
'field_1': value_1,
'field_2': value_2,
})

2. Copy:

* Used to duplicate records. 

* This method creates a new record with the same data as the original, but it allows you to customize certain fields, such as removing or altering fields that should not be copied.

* Model.copy(default=None)

new_record = original_record.copy(default=None)

3. Write

* Updates existing records with the given data.

* Model.write(vals)

* In the example, the field email of the partner record gets updated by the given value.

* Accepts a dictionary with fields to update and the value to set on them.

partner.write({ 'email': 'new.email@example.com', })

4. Browse

* Used to fetch records from a database for a model using their IDs given as parameters.

* Model.browse([ids]) ? records

* In the example, browsing inside the ‘res.partner’ model for the record with id, partner_id.

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

5. Search

* Searches for records that meet certain conditions and returns a recordset.

* In the example, searching inside the ‘res.partner’ model for the records that meet the condition, partner name as ‘John Doe’.

* Model.search(domain[, offset=0][, limit=None][, order=None])

* Use an empty list to match all records.

partners = self.env['res.partner'].search([('name', '=', 'John Doe')])

6. SearchCount

* Returns the number of records that match the given domain.

* Model.search_count(domain[, limit=None])

* In the example, returns the count of active records in the ‘res.partner’ model.

count = self.env['res.partner'].search_count([('active', '=', True)])

7. Read

* Reads specific fields of a recordset and returns a list of dictionaries with the field values.

* Model.read([fields])

* In the example, partner_data will be a list containing field values, name, and email of all partner records.

partner_data = partner.read(['name', 'email'])

8. Unlink

* Delete records from the database.

* In the example, the row corresponding to the partner record will be removed from the database.

* This method permanently deletes records, so it should be used carefully.

partner.unlink()

9. Flush

* The flush method is an ORM function that forces the immediate writing of any pending changes in the current transaction to the database.

* The given example is a method used in Odoo to manually flush the SQL query cache and ensure that any pending database operations or changes in the Odoo ORM (Object-Relational Mapping) are committed to the database.

self.env.cr.flush()

10. Map

* Mapped is used to get the values of a specific field from a recordset. 

* In the example, we are retrieving a list containing ‘invoice_ids’ of all records in the ‘sale.order’ model.

invoices = self.env['sale.order'].mapped('invoice_ids')

11. Sort

* The sorted() is used to sort a recordset based on specific fields, which returns a new recordset ordered by the specified field.

* In the example, the ‘line_ids’ get sorted based on the name of products in each line.

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

Conclusion

Essential methods like create, write, and unlink streamline data manipulation, while advanced methods such as search, read, and browse facilitate efficient querying and retrieval of records. With tools like mapped, sorted, etc, developers can handle recordsets dynamically and perform complex operations with ease. Odoo18's ORM not only enhances productivity but also ensures that developers can focus more on business logic rather than database intricacies, making it an invaluable component of the framework.

To read more about ORM Methods in Odoo 17, refer to our blog ORM Methods in Odoo 17.


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



0
Comments



Leave a comment



whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message