Openerp中的Onchange函数

Pra*_*a V 3 python xml onchange openerp

我在account.invoice.line中有一个名为form_type的选择字段.它有三个选择选项:

1) form_a
2) form_b
3) form_c
Run Code Online (Sandbox Code Playgroud)

account.invoice.line中还有一个名为flag的整数字段.选择form_c时,标志值应设置为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}
Run Code Online (Sandbox Code Playgroud)

我在onchange的account.invoice.line中的XML代码是:

<field name="form_type" on_change="onchange_form_type(form_type)"/>
Run Code Online (Sandbox Code Playgroud)

Sam*_*nde 6

在您的on-change函数中,您不需要调用对象的browse函数,因为这些值尚未存储在数据库中.此外,您将"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}
Run Code Online (Sandbox Code Playgroud)