Onchange function in Openerp

戏子无情 提交于 2019-12-12 13:33:17

问题


I have a selection field in account.invoice.line named form_type. It has three selection options:

1) form_a
2) form_b
3) form_c

There is also an integer field named flag in account.invoice.line. When form_c is selected, the flag value should be set to 1; otherwise, if either form_a or form_b is selected, the flag value should be set to 0. I wrote an onchange function for the above case but it's not working. Can someone help me out? What is wrong in my code?

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}

My XML code in account.invoice.line for onchange is:

<field name="form_type" on_change="onchange_form_type(form_type)"/>

回答1:


In your on-change function you don't need to call the browse function of the object, because the values are not stored in the database yet. Also, you are passing the "form_type" value to the function and not the object id(as browse accepts object id).

So, below will be the on_change function, for the expected requirement:

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}


来源:https://stackoverflow.com/questions/10256227/onchange-function-in-openerp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!