Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Onchange method on a one2many field

Yassine TEIMI
on 12/29/14, 12:36 PM 1,371 views

As you all know, Customer payments have 2 technical components : Account_voucher and Account_voucher_line.

On Customer Payments two fields interests me for the moment :

The amount field of account_voucher.

The amount field of account_voucher_line.

There is an onchange method on the field "amount" of account_voucher_line,

    def onchange_amount(self, cr, uid, ids, amount, amount_unreconciled, context=None):
        vals = {}
        if amount:
            vals['reconcile'] = (amount == amount_unreconciled)
        return {'value': vals}


it checks true if the amount is reconcilied, I want to return on the same onchange method the sum of every amount payed, for every voucher line, on the field amount of account.voucher.

So it's an onchange method from account_voucher_line into account_voucher.

I've tested a code, but it reacts on the field "amount" of account_voucher_line and not account_voucher.

It seems that there is a limitation, because, the onchange method works only on fields of the same model I guess.

Any recommandations for this issue ?

Mohammed Mansour
On 2/18/15, 9:34 AM

You can solve this issue by following this workaround solution:

def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
        result = {'value': {'emp_contract_ids': False, 'emp_contract_ids_list': None}}
        emp_contract_ids = self.pool.get('hr.contract').search(cr, uid, [('employee_id', '=', employee_id)])
        if emp_contract_ids:
            result['value'] = { 'emp_contract_ids': emp_contract_ids, 'emp_contract_ids_list': emp_contract_ids}
        return result 
        'employee_id': fields.many2one('hr.employee', 'Employee', required=True),
        'emp_contract_ids': fields.one2many('hr.contract', 'employee_id', 'Employee Contracts', readonly=True), # a field just for user to see the results in the one2many widget as you want.
        'emp_contract_ids_list': fields.char('Employee Contracts ID List', invisible=True) # this field only to save the returned IDs from the onchange function and by then you can use it simply like that:


emp_contract_ids_list = map(int, emp_contract_ids_list.split(','))
for contract in self.pool.get('hr.contract').browse(cr, uid, emp_contract_ids_list):


Abdullah Sofan
On 12/29/14, 2:40 PM

There is a known bug with on_change and one2many field. Which version are you using?



version 7

Yassine TEIMI
on 12/29/14, 2:47 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 12/29/14, 12:36 PM
Seen: 1371 times
Last updated: 3/16/15, 8:10 AM