Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
834 Vues

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
Ignorer
Meilleure réponse

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
Ignorer

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

Auteur

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.

Publications associées Réponses Vues Activité
1
oct. 17
6921
2
janv. 19
14845
0
juin 18
9002
1
juin 25
15362
3
avr. 25
5532