On Change (on_change)
The on_change is the attribute that describes what will happen when a field value changes, what changes in other fields or an event will take place.
Syntax
<field name="field_name" on_change="function_name(field1, field2,..., fieldN)"/>
The function defined here will take the usual arguments that any function in python takes. Yo need to define all the fields in the arguments list which will be changing in the view.
Example
Add the field:
'journal_id': fields.many2one('account.journal', 'Journal', required=True, readonly=True, states={'draft':[('readonly',False)]}),
add the onchange_ function:
def onchange_journal_id(self, cr, uid, ids, journal_id=False, context=None):
result = {}
if journal_id:
journal = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context)
currency_id = journal.currency and journal.currency.id or journal.company_id.currency_id.id
result = {'value': {
'currency_id': currency_id,
}
}
return result
Map the function to the field in the view:
<field name="journal_id" on_change="onchange_journal_id(journal_id)" widget="selection"/>
Explanation:
This example shows that a journal_id i.e a journal when selected will return a currency associated with that journal in the field currency_id. So as this journal changes the onchange_journal_id function will return a dictionary (with a number of fields added in the function) to the fields in the view here is the field currency.
Every time a field is changed new associated field values you want to changed in the view can be returned using this way.
To make it even more simpler, every time you select a new customer, you would like to return his address, phone, and email id associated to the address, phone and email fields.
0 Comment(s)