コンテンツへスキップ
メニュー
この質問にフラグが付けられました
3 返信
16636 ビュー

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



アバター
破棄
最善の回答

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

アバター
破棄
最善の回答
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)


アバター
破棄

Thanks :)

最善の回答

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
アバター
破棄
関連投稿 返信 ビュー 活動
3
2月 25
3372
0
5月 24
46
1
4月 24
3280
4
9月 23
4755
2
9月 23
6974