Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
42126 Lượt xem

Hello,


Could anyone explain how ORM API works with write() and update().  

AFAIK, CRUD represents create, read, update, delete which corresponds to create(), read(), update(), unlink() in ORM.

However, the explanation on odoo 13.0 documentation (\https://www.odoo.com/documentation/13.0/reference/orm.html)

write() seems to be the one to update a existing record.  

update() can do the same, and even on psedo-records that are not created yet.  

If that is the case, what are the scenarios to use write() over update()?

Ảnh đại diện
Huỷ bỏ

Hi Guys
Please can have a example of using write() and update() ?
odoo 13

Câu trả lời hay nhất

Hi,

Write() Updates all records in the current set with the provided values, but write() is one of the CRUD methods that do not work for records set which are not present in the database. An onchange method returns pseudo-records which do not exist in the database yet. So set record's field using update() method as calling write() method gives an undefined behavior.

Regards

Ảnh đại diện
Huỷ bỏ
Tác giả

If I understand you correctly.

1. CRUD, is write(), not update(), odoo ORM naming the way it is.

2. write() can update multiple records and its corresponding fields at the same time.

3. update() is designed to work with pseudo-records.

The only benefit of write() is the ability to update multiple records. Otherwise use update() in custom function?

I make this conclusion because update() can update existed records as well. I've not tried update() multiple yet though.

When setting multiple fields at the same time use write() so that if it takes a number of field values the function writes them to all the records in its recordset. self.write({'name':'Name', 'age': age})

So gives you single update statement in the database.

But update() only works on singletons and performs a database write for each written field.So at a time it updates only single record.It Iterates through the fields and sets each one separately giving you multiple update statements in the database.

for order in self:

amount_untaxed = amount_tax = 0.0

for line in order.order_line:

amount_untaxed += line.price_subtotal

amount_tax += line.price_tax

order.update({

'amount_untaxed': amount_untaxed,

'amount_tax': amount_tax,

'amount_total': amount_untaxed + amount_tax,

})

and it can be used within onchange functions as they return pseudo records.ie.update function can work on records not present in database yet.

Tác giả

Thank you very much for the clears up.

I was doing it wrong by abusing for-loop with update().

Câu trả lời hay nhất

Hello
in write method , all the record has been updated of model and in update method, only particular filed of record you can update. This is the main difference between write and update method.

Ảnh đại diện
Huỷ bỏ
Tác giả

thank you for the explanation.

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 11 19
3473
3
thg 4 17
18413
1
thg 2 16
4102
1
thg 8 25
398
1
thg 8 25
657