Development Book V18: Create and update multiple records

For those new to Odoo development, it's quite common to encounter scenarios requiring the creation of multiple records at once.

To handle this efficiently, Odoo provides support for batch record creation. Rather than supplying a single dictionary representing one record, you can pass a list of dictionaries, with each dictionary defining a different record. This approach streamlines the creation process and enhances performance.

vals = [{'name': "Azure", 'age': '20',},
{'name': "Deco", age: 21},
{'name': "Ready", age: '19',}]
self.env['student.student].create(vals)

When working with multiple records in Odoo, it's important to be mindful of method differences between versions. Odoo uses a lazy update mechanism, meaning that changes made to records aren't immediately written to the database. Instead, the system defers writing until it's required, or until a manual call to flush() is made.

For example:

data1 = {...}
for record in recordset:
    record.write(data1)

data2 = {...}
recordset.write(data2)

To efficiently create multiple records in Odoo, you can pass a list of dictionaries to the create() method. Each dictionary represents a record, allowing Odoo to process them in a batch. Although each record is inserted through individual queries, this method improves performance by avoiding unnecessary overhead and reduces the impact of logging each record creation separately.

When updating records, Odoo’s ORM handles most of the complexity. If you're applying the same changes to several records, the system consolidates the operation into a single UPDATE query, improving efficiency. Additionally, the framework smartly handles repeated edits to the same record within one transaction, ensuring optimal database performance and consistency.

recordset.name= 'Name'
recordset.email= 'name@example.com'
recordset.name= 'admin'
recordset.email= 'admin@example.com'

In the given code example, a single write() operation is performed, updating fields like name='admin' and email='admin@example.com'. Odoo consolidates these changes into one efficient UPDATE query, ensuring minimal database interaction and maintaining optimal performance. This streamlined approach reduces overhead and improves the speed of bulk updates.

Generate records through database query

While Odoo's ORM provides a streamlined and structured way to interact with the database, its capabilities for complex data generation and transformation are somewhat limited. Retrieving data in a custom format often requires additional processing on the recordsets, which can introduce performance overhead.

In scenarios where precise control or performance optimization is critical, executing raw SQL queries directly against the database becomes a viable option. However, this approach should be used with care and in alignment with Odoo’s best practices for executing SQL queries.

There is a common belief within the Odoo developer community that raw SQL queries are always faster than ORM methods. This assumption, however, is not universally true. In many cases, the ORM is more efficient due to its use of internal caching and optimized recordset operations. Therefore, choosing between SQL and ORM should be context-driven.

Odoo offers two primary ways to execute SQL queries within the model:

  • Using self._cr
  • Using self.env.cr

Here’s an illustration of how to use self.env.cr to execute raw SQL queries in Odoo:

self.env.cr.execute("Select * from book_book where name like '%s'" %(search_keyword,))

Explore examples that demonstrate how self._cr is used in Python code within Odoo to execute direct SQL queries.

self.env.cr.execute("Select * from book_book where name like '%s'" % ('%War and peace%',))
result = self.env.cr.fetchall()

The result is returned in a dictionary format. If you need to fetch just a single record instead of multiple, you can use fetchone() or dictfetchone(). These methods work similarly to fetchall() and dictfetchall(), but return only one record from the query result.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message