Search code examples
odooonchangeodoo-8invoiceaccounting

Odoo 8: onchange many2one not working


under accounting->invoice I'm trying to trigger the onchange uppon selecting a customer(field: partner_id: many2one) from the list , but it fails, whereas adding the onchange decorator on the field "origin" (type: char) works normally. can anybody help?

NB: in Odoo debugging mode the help message shown upon dragging the mouse on customer field that it's binded to an onchange function called: onchange_partner_id(type,...), I wonder if this is the cause of the issue

Here is the code: I inherit from the original invoice model than adding the onchange functions

class stock_picking(models.Model):
_inherit = "account.invoice"

#NOT triggered
@api.onchange('partner_id')
def _onchange_customer(self):
    print("debug:y_account_invoice: _onchange_customer:selected")

#triggered successfully    
@api.onchange('origin')
def _onchange_origin(self):
    print("debug:y_account_invoice: _onchange_origin")

Solution

  • You just need to override this method in py.

    @api.multi
    def onchange_partner_id(self, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
        res = super(classname, self).onchange_partner_id(type, partner_id, date_invoice=date_invoice,payment_term=payment_term, partner_bank_id=partner_bank_id, company_id=company_id)
        #### Update your code 
        # If you want to set any fields value then just simply update it in res and return res
        res['value'].update({'account_id': new_value,})
        return res
    

    onchange_partner_id is already there you need to override it don't define it again. And _onchange_origin working in your case because it's not there already.