Without xml possible on change method in openerp

94 Views Asked by At
def onchange_partner(self, cr, uid, ids, partner_id, context=None):
    res = {}
    if partner_id:
        obj = self.pool.get('res.partner').browse(cr, uid, partner_id)
        res['field1'] = obj.field1
        res['field2'] = obj.field2
        res['field3'] = 'Hello' # this field type must be char or if not then give 
        #value accordingly, its just to prove you that values are filled on onchange.
    return {'value': res}

def onchange_partner(self, cr, uid, ids, partner_id, context=None):

but i am not write "onchange_partner" because it using without xml how it

1

There are 1 best solutions below

0
On

There is two way to write onchange method.

Old Api :

in model.py

def myonchange(self,cr,uid,ids,field_name,context=None):
    ...
    ...
    ... 
    return {'value':{'field': 'value'}}

in model_view.xml

<field name="field_name" on_change="myonchange(field_name)"/>

if you are using old api you need to add onchange attribute on field in xml view.

New Api :

@api.onchange('field_name')
def my_onchange(self):
      self.field_name = 'Value'  # to set filed value

in new API you need to add decorator which trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form.