This question has been flagged
2 Replies
2683 Views

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
Best Answer

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.


Avatar
Discard
Author

Ah ok, thank you!

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

Avatar
Discard
Author

I didn't knew that one, thanks!

Author

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

on the XML add the force_save="1" attribute which you are trying to save the value.