Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
811 Vistas

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


Avatar
Descartar
Mejor respuesta

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.

Avatar
Descartar

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).

Autor

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.

Publicaciones relacionadas Respuestas Vistas Actividad
1
oct 17
6912
2
ene 19
14822
0
jun 18
8980
1
jun 25
15340
3
abr 25
5473