我在 account.invoice.line 中有一个名为 form_type 的选择字段。它具有三个选择选项:
1) form_a
2) form_b
3) form_c
account.invoice.line 中还有一个名为flag 的整数字段。选择form_c时,flag值应设置为1;否则,如果选择了 form_a 或 form_b,则标志值应设置为 0。我为上述情况编写了一个 onchange 函数,但它不起作用。有人可以帮我吗?我的代码有什么问题?
def onchange_form_type(self, cr, uid, ids, invoice, context=None):
val={}
flag=0
invoice = self.pool.get('account.invoice.line').browse(cr, uid, invoice)
for invoice in self.browse(cr, uid, ids, context=context):
if invoice.form_type=="form_c":
flag="1"
else:
flag="0"
print flag
val = { 'flag': flag, }
return {'value': val}
我在 account.invoice.line 中用于 onchange 的 XML 代码是:
<field name="form_type" on_change="onchange_form_type(form_type)"/>
最佳答案
在您的更改函数中,您不需要调用对象的浏览函数,因为值还没有存储在数据库中。此外,您将“form_type”值传递给函数而不是对象 ID(因为浏览器接受对象 ID)。
因此,下面将是 on_change 函数,用于预期的要求:
def onchange_form_type(self, cr, uid, ids, form_type, context=None):
val={}
flag=0
if form_type == 'form_c':
flag="1"
val = { 'flag': flag }
return {'value': val}
关于python - Openerp 中的 onchange 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256227/