콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
16661 화면

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
3390
0
5월 24
46
1
4월 24
3284
4
9월 23
4760
2
9월 23
6979