blog

orm-methods-in-odoo
News & Events

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

  1. 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”})

  1. write()

The write () method is used to update the values of the record set. Specify the data dictionary and updated 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

  1. search()

 This method is used to search records within the model based on the search domain which is passed. In this method pass arguments.

Request Your Free Quote

Example:

Here shows 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

  1. browse()

This method used to browse record sets with ID

Example: self.env[“res.partner].browse(id)

  1. copy()

The copy() method is used to duplicate a data record in the current model.

Example: self.copy()

  1. 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

  1. 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

  1. ensure_one()

The insertion() method checks whether the record is single

If not single raises an exception.

Example: self.ensure()

  1. 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

  1. 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

  1. 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

Leave a Reply

Your email address will not be published.

Open chat
1
Hello, can we help you?