Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
16066 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore
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
Abbandona

Thanks :)

Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
feb 25
2468
0
mag 24
46
1
apr 24
2635
4
set 23
4068
2
set 23
6356