in this example I am iterating on all records in the model ,but way this iteration if i want to compute the value of field in the current record
@api.depends("age")
def _computeHalfAge(self):
for record in self:
record.age_days = record.age * 365
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
in this example I am iterating on all records in the model ,but way this iteration if i want to compute the value of field in the current record
@api.depends("age")
def _computeHalfAge(self):
for record in self:
record.age_days = record.age * 365
In this case, 'self' is a recordset. Think of it as an array of records. If you log 'self' before the for loop, you should see something like Model(10,23,54), the numbers being the DbIds for the records present in the recordset.
You're doing the iteration correctly. When model.age changes in 1 or many records, the method _computeHalfAge will be called like so: model._computeHalfAge(Model(id_1, id_2,id_3)) for every record in the recordset. Then, the 'record' variable will be each record in the recordset while iterating.
Good answer. The compute method has to work on both the current record as well as a group of records, since in Odoo there are ways to edit muiltiple records (Multi-edit, Import, Server Action, etc).
Thanks Cédric and Ray ,but is that editing all records is efficient ? Imagine i have 10 Millions records ..... so why the compute function is going to compute 10 Millions times (whether the field is stored or not)?
Ideally, you wouldn't be iterating on all records on a certain model all at once all the time. You would iterate over the subset (recordset) of records that had a change in the "age" field. In any case, computers are very fast at multiplication (and basic arithmetic operations in general) and, while I've never dug through the database interface, I'm almost certain Odoo's ORM has a batched read and write mechanism to retrieve and change the whole recordset at once in the database, reducing the overhead cost of I/O for the operation. If you think about it, every time you open a view, you have to fetch all the records with all their stored fields to check against the domain and filters to keep the desired one. That's a lot more read operation than what you're doing here.
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
1
Oct 17
|
6350 | ||
|
2
Jan 19
|
13565 | ||
|
0
Jun 18
|
8168 | ||
Compute Fields
Solved
|
|
2
Jul 24
|
927 | |
|
1
Jan 24
|
730 |