Help

0

Can computed fields be slow if the table is huge?

Avatar
Remco Huijdts

Hello, I am an odoo beginner and have a question:


The way computed fields work seems to be:

from odoo import api
total = fields.Float(compute='_compute_total')

@api.depends('value', 'tax')
def _compute_total(self):
    for record in self:
        record.total = record.value + record.value * record.tax


But it seems to me that is code always loops over all the records, is that correct?

Doesn't that make the code slow on large tables? Are there other methods only working on the current record?

Regards,


 

Avatar
Discard
2 Answers
0
Best Answer

 Hello Remco Huijdts,

Use the onchange  method to apply the change on the current form.


Example:

    @api.onchange('value', 'tax')

    def _onchange_total(self):

        if self.value or self.tax:

            self.total = self.value + self.value * self.tax


Thanks..

For more information Contact us:-https://kanakinfosystems.com/odoo-development-services

2 Comments
Avatar
Discard
Avatar
Remco Huijdts
-

I didn't knew that one, thanks!

Avatar
Remco Huijdts
-

I tried it but it seems not possible to save the calculated field?

0

It depends on how you are using the compute fields. In your case, your method will only be called when "value" and/or "tax" fields are updated because you have added these two fields in the api.depends.


1 Comment
Avatar
Discard
Avatar
Remco Huijdts
-

Ah ok, thank you!