Skip to Content
Menu
This question has been flagged
3 Replies
15053 Views

Hi, I can help you with this problem that I have. Can multiple values ​​be calculated using the same python function? To simplify, suppose we have two numbers, can the product and division be calculated within the same function? I try to do this, compile well inside Odoo but when giving values ​​in the view I get the error that multiplication is not defined

 num_uno=fields.Integer("Value 1")
num_dos=fields.Integer('Value 2')
campocalculado = fields.Float(string='Calculated field', compute='multiplicar')
campocalculado2 = fields.Float(string='Calculated field', compute='dividir')

@api.onchange('num_uno', 'num_dos')
def _computeVar(self):
for record in self:
record.multiplicar = record.num_uno * record.num_dos
record.dividir = record.num_dos / record.num_uno

I know that in the field of the calculated field it must have the name of the function and not that of the record. But since they are multiple values, I don't know what to do to read the values

Any thoughts are welcomed.

Thanks



Avatar
Discard
Best Answer

Hi Omar,

It can be done with the following code:

num_uno=fields.Integer("Value 1")
num_dos=fields.Integer('Value 2')
multiplicar = fields.Float(string='Multiplicar', compute='_computeVar')
dividir = fields.Float(string='Dividir field', compute='_computeVar')

@api.depends('num_uno', 'num_dos')
def _computeVar(self):
for record in self:
record.multiplicar = record.num_uno * record.num_dos
record.dividir = record.num_dos / record.num_uno

The value of the compute parameter must be the name of the function to call, not the name of the variable. The function then directly changes the value of the field.

Cheers
Jake Robinson

Avatar
Discard
Best Answer
ZeroDivisionError :)

num_uno=fields.Integer("Value 1")
num_dos=fields.Integer('Value 2')
multiplicar = fields.Float(string='Multiplicar', compute='_computeVar')
dividir = fields.Float(string='Dividir field', compute='_computeVar')

@api.depends('num_uno', 'num_dos')
def _computeVar(self):
for record in self:
record.multiplicar = record.num_uno * record.num_dos
record.dividir = record.num_dos / (record.num_uno and record.num_uno or 1)


Avatar
Discard

Thanks :)

Best Answer

You can't use onchange while compute the two or more fields you should mention the fields that your going to depends on which is @api.ondepend(), it can be done in this way


num_uno=fields.Integer("Value 1")
num_dos=fields.Integer('Value 2')
multiplicar = fields.Float(string='Multiplicar', compute='_computeVar')
dividir = fields.Float(string='Dividir field', compute='_computeVar')

@api.depends('num_uno', 'num_dos')
def _computeVar(self):
for rec in self:
rec.multiplicar = rec.num_uno * rec.num_dos
rec.dividir = rec.num_dos / rec.num_uno
Avatar
Discard
Related Posts Replies Views Activity
3
Apr 24
1024
0
May 24
46
1
Apr 24
1830
4
Sep 23
3085
2
Sep 23
5593