Blogs and Latest News

Welcome to our blog, where insights meet innovation! Dive into our latest articles to explore the cutting-edge trends and strategies shaping the business world.
bt_bb_section_bottom_section_coverage_image

Comprehensive Guide to ORM Methods in Odoo 18

Introduction

In Odoo, ORM (Object-Relational Mapping) methods are at the core of database interaction, enabling developers to seamlessly create, read, update, and delete records within models. These ORM methods simplify the complexity of SQL queries, allowing developers to concentrate on business logic rather than database management..

 

Basic ORM Methods in Odoo

 

Create
  • Used to create new records in the database.
  • Model.create(vals_list)
  • Accepts a list of dictionaries containing field values.
  • Returns a recordset (a list of new records created).

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

 

Copy
  • Duplicates records, allowing customization of certain fields.
  • Model.copy(default=None)

new_record = original_record.copy(default=None)

 

Write
  • Updates existing records with new data.
  • Model.write(vals)
  • Accepts a dictionary of fields and their new values.

partner.write({’email’: ‘[email protected]’})

 

Browse
  • Fetches records by their IDs.
  • Model.browse([ids])

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

 

Search
  • Searches for records that meet specific conditions.
  • Model.search(domain[, offset=0][, limit=None][, order=None])

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

 

Search Count
  • Returns the number of records matching the given domain.
  • Model.search_count(domain[, limit=None])

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

 

Read
  • Reads specific fields from a recordset, returning a list of dictionaries.
  • Model.read([fields])

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

 

Unlink
  • Deletes records from the database permanently.
  • Use carefully as it removes the records.

partner.unlink()

 

Flush
  • Forces the immediate writing of any pending changes to the database.
  • Useful for manually committing SQL queries or pending operations.

self.env.cr.flush()

 

Mapped
  • Retrieves values of a specific field from a recordset.
  • Useful for extracting lists of field values.

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

 

Sorted
  • Sorts a recordset by specified fields, returning a new ordered recordset.

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

 

Conclusion

Odoo’s ORM methods, such as create, write, and unlink, make data management straightforward, while methods like search, read, and browse enable efficient querying. Advanced functions like mapped and sorted allow developers to handle recordsets dynamically, making ORM in Odoo 18 a powerful asset for building complex business logic effortlessly.

 

 

About us

We are Timus Consulting Services, a fast-growing, premium Governance, Risk, and compliance (GRC) consulting firm, with a specialization in the GRC implementation, customization, and support.

Our team has consolidated experience of more than 15 years working with financial majors across the globe. Our team is comprised of experienced GRC and technology professionals that have an average of 10 years of experience. Our services include:

  1. GRC implementation, enhancement, customization, Development / Delivery
  2. GRC Training
  3. GRC maintenance, and Support
  4. GRC staff augmentation

 

Our team

Our team (consultants in their previous roles) have worked on some of the major OpenPages projects for fortune 500 clients across the globe. Over the past year, we have experienced rapid growth and as of now we have a team of 15+ experienced and fully certified OpenPages consultants, OpenPages QA and OpenPages lead/architects at all experience levels.

 

Our key strengths:

Our expertise lies in covering the length and breadth of the IBM OpenPages GRC platform. We   specialize in:

  1.  Expert business consulting in GRC domain including use cases like Operational Risk   Management, Internal Audit Management, Third party risk management, IT Governance amongst   others
  2.  OpenPages GRC platform customization and third-party integration
  3.  Building custom business solutions on OpenPages GRC platform

 

Connect with us:

Feel free to reach out to us for any of your GRC requirements.

Email: [email protected]

Phone: +91 9665833224

WhatsApp: +44 7424222412

Website:   www.Timusconsulting.com

Share

Nikhil