Blog

News & Events

ORM Methods and Decorators in Odoo16 | Odoo ORM Methods

HARSHAD
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”})
 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
  1. search_count()
 This method returns the number of records in the search method.  Example:
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()
  1. Unlink()
The unlink() method is used to delete the data record from the database. Example:
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
  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 :
 self.env[‘hr.employee’].search([(‘name’,’=’,’ABC’)]).mapped(‘country_id.name’)
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  :
@api.depends('state')

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  :
@api.onchange('state')

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. 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
  1. API.model
This decorator will convert old API calls to the decorated functions to a new API signature
@api.model

          def create(self,vals):

                   res = super().create(vals)

                   res.update({“name”:”ABC”})

                   return res

DOWNLOAD ERP

Latest Posts

Create On-Site Payments & Picking With Odoo 16

September 19, 2023

Scalable ERP Software in Real Estate Services | Visit Us at the Gitex 2023

September 18, 2023

Analysis of HR Organizational Chart in Odoo 16 

September 15, 2023

Creating a Settings Menu in Odoo Custom module

September 14, 2023

Leave a Reply

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