Skip to Content
Menu
This question has been flagged
1 Reply
1696 Views

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
Discard
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
Discard
Related Posts Replies Views Activity
0
Aug 24
937
1
Mar 22
4464
1
Jun 21
2919
0
Mar 21
1732
0
May 20
2201