Overview of ORM (Object Relational Mapping) Methods in Odoo15
ORM stands for Object Relational Mapping. With the help of ORM methods, fill the gap between Open-objects and SQL tables. Object Relational Mapping helps to perform SQL queries. In Odoo, ORM methods help to create, update, delete, and search records in the database. ORM methods help to reduce development time and cost.
Common ORM Methods
- create()
The create() method is used to create new data records in the database. First, specify the model and define a dictionary.
Example: Here showing an example of partner creation
self. env[‘res. partner’].create({‘name’:”Test”})
-
write()
The write () method is used to update the values of the record set. Specify the data dictionary and update the record with these values.
Example:
Here showing an example of partner updation
self.write({name: “Test”})
in the above example, the record name becomes changed to Test
-
search()
This method is used to search records within the model based on the search domain which is passed. In this method pass arguments.
Example:
Here is an example of a Partner searching with the name
self.env[“res. partner”].search([(“name”,”=”,”Test”)])
In the above example return Test named record sets
-
browse()
This method used to browse record sets with ID
Example: self.env[“res.partner].browse(id)
- copy()
The copy() method is used to duplicate a data record in the current model.
Example: self.copy()
-
search_count ()
This method is used to get several records in the search method.
Example: self.env[‘res.partner’].search_count([(‘is_company’,’=’,True)])
This returns the total number of partners in your database
- unlink()
The uplink method is used to delete/remove the data record from the model and database.
Example: self.unlink()
In the above example, the current record becomes deleted
-
ensure_one()
The insertion() method checks whether the record is a single
If not single raises an exception.
Example: self.ensure()
-
filtered()
This method used to filter records in a specific condition
Example:
self. env[‘sale. order’].filtered(lambda x:x.state == “sale”)
This returns a list of sale orders with the sale order state
-
mapped()
This method returns the field values
Example: self.env[‘sale.order’].search([(‘state’,’=’,’sale’)]).mapped(‘partner_id’)
In the above example search sell orders with state sell order and mapped by partner_id, this returns the partners list from the sale order
-
sorted()
The method used to sort records into a specific condition
Example:
self. env[‘res. partner’].sorted(key=lambda r: r.name)
It sorts the above example partner records into the name
Method decorators in Odoo
ORM Methods in Odoo have different decorators. This decorator changes the behavior of methods.
- model()
- depends(arguments)
- constraints(arguments)
- on change(arguments)
DOWNLOAD ERP
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote