Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
2079 Widoki

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?

Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
sie 24
1245
1
mar 22
4858
1
cze 21
3230
0
mar 21
2021
0
maj 20
2437