Blog

News & Events
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
-
- create()
self.env [‘hr. employee’].create({‘name’:”Employee One”})2.write() 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”})3.search() 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.
self.env[“hr.employee”].search([(“name”,”=”,”ABC”)]) It returns the employee records named ABC
- search_count()
self.env[‘hr.employee’].search_count([(‘name’,’=’,”ABC”)])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 :self.env[“hr,employee”].browse(id)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. Example:
self.copy()
- Unlink()
self.unlink()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. Example:
self.ensure()In the above eg check in the current record is single or not
- filtered()
eg : self.env[‘hr.employee’].filtered(lambda x:x.name == “ABC”)In the above eg returns list of an employee named ABC
- mapped()
self.env[‘hr.employee’].search([(‘name’,’=’,’ABC’)]).mapped(‘country_id.name’)The above example returns the country of an employee whose name is “ABC”.
- sorted()
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 :@api.depends('state') def _compute_status(self): for record in self: record.status = record.stateIn 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 :
@api.onchange('state') def _onchange_status(self): for record in self: record.status = record.stateThis 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. 3.api.constraints eg :
@api.constraints('state') 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
- API.model
@api.model def create(self,vals): res = super().create(vals) res.update({“name”:”ABC”}) return res