Skip to Content
Menú
This question has been flagged
1 Respondre
1692 Vistes

I'm executing a function in a cron job and fetch a certain partner from the database.

Suppose I change the partners name in that function.

partner = self.env[...].sudo().search(...)  #assumes this works and returns object

partner.name = 'New name'

I notice that after I change the property, the write function is called (I put a breakpoint in the overwritten write() method). My question is: who triggers this write()? And, suppose I do two changes on this object, e.g. also change something in the address, will this then cause two write() calls, and, thus also two DB updates. 

We can however also call partner.write({'name': 'New name'}). What is the difference and what is the preferred way of working?

Avatar
Descartar
Best Answer

Hi Wim,

The ORM or Object-Relational Mapping triggers this write().

If you want to know more about the ORM, you can read the models.Model class definition. 

As you said, calling write directly will ensure you can pass all the values you want to write and they will all be applied in a single call.

Little twist: is partner is a recordset of over 1000 records, the SQL queries will be by batches of 1k. Keep in mind search() returns recordsets, not records. You can use limit=1 as a parameter to ensure it will be a set of maximum size 1.

Feel free to check out what happens when you make successive assignments: is it one or multiple calls to write() ? Easy enough with a breakpoint, as you already did.

For reference and since it's a bit related, the different between write and update. When you do an assignment, Python will automatically call the correct method.

Avatar
Descartar
Related Posts Respostes Vistes Activitat
0
d’ag. 24
924
1
de març 22
4460
1
de juny 21
2913
0
de març 21
1718
0
de maig 20
2194