Create and update multiple records
If you are a new user in Odoo development, you have to execute multiple queries to write
or create multiple records, In this case, we can take a look at how to create and write
records.
First check how to Creating multiple records.
Odoo allows you to create records in batches.Simply give a dictionary with the field
values if you're generating a single record.Instead of passing a single dictionary, you
can pass a collection of these dictionaries to create records in a batch.In a create
methode, the following example creates single records.
value = [{'name': "Anu",'age': '10',},
{'name': "Ammu",age: '20'},
{'name': "Appu",age: '18’,}]
self.env['school.student].create(value)
Writing multiple records.
If you are working on different versions of Odoo, you should be aware of how to write
method works in this case.It uses a delayed approach for updates, which means that it
does not write data in the database immediately. Odoo writes the data to the database
only when necessary or when flush() is called.
For example:
data1 = {...}
for record in recordset:
record.write(data1)
data2 = {...}
recordset.write(data2)
To produce numerous records in a batch, you must use value dictionaries in the form of a
list.This will take care of batch-creating the records for you.
When you produce records in a batch, a query is automatically inserted for each
record.This means that batch records are not created in a single query.However, this
does not negate the fact that batching records improves performance.Batch-calculating
computing fields provide the performance boost.
For the write technique, things are a little different.The framework is responsible for
the majority of the effort.For example, if you write the identical data on all records,
you can update the database with just one UPDATE query.
Even if you edit the same record multiple times in the same transaction, the framework
will handle it:
recordset.name= Name
recordset.email= name@example.com'
recordset.name= Admin
recordset.email= Admin-2@example.com'
Only one query will be run for write in the example code, with the resulting values of
name=Admin and email=Admin-2@example.com.Assigned values are written in later in a
single query, this has no negative impact on performance.