Odoo Help

1

Error while trying to extend onchange method in account.invoice

By
E.M.
on 9/20/15, 1:51 PM 1,564 views

I am trying to extend account.invoice with a new field which is automatically populated when customer is selected in the invoice form.

So far I am not being able to call the inherited onchange_partner_id() method with super method.

Any help is welcomed.

Code is:

# -*- coding: utf-8 -*-
from openerp import models, fields, api, exceptions

class res_partner(models.Model):

_inherit = "res.partner"
payment_method = fields.Selection(
selection=[
('bank_transfer', 'Transferencia Bancaria'),
('check', 'Cheque'),
('confirming', 'Confirming'),
('bank_giro', 'Giro'),
('promisory_note', 'Pagaré'),
('direct_debit', 'Domiciliación')
],
string='Método de pago')

class account_invoice(models.Model):

_inherit = "account.invoice"
payment_method = fields.Selection(
selection=[
('bank_transfer', 'Transferencia Bancaria'),
('check', 'Cheque'),
('confirming', 'Confirming'),
('bank_giro', 'Giro'),
('promisory_note', 'Pagaré'),
('direct_debit', 'Domiciliación')
],
string='Método de pago')

@api.model
def onchange_partner_id(self, vals):

#payment_method = res.partner.payment_method
#payment_method = ['test','TEST!']
return super(account_invoice, self).onchange_partner_id(vals)

But unfortunately I am getting the following error:

Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 574, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 310, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 307, in checked_call
return self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 803, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 944, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 363, in old_api
result = method(recs, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5864, in onchange
record._onchange_eval(name, field_onchange[name], result)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5780, in _onchange_eval
method_res = getattr(self._model, method)(*args, context=self._context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 335, in old_api
recs = self.browse(cr, uid, [], context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5224, in browse
return self._browse(Environment(cr, uid, context or {}), ids)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 713, in __new__
self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context))
ValueError: dictionary update sequence element #0 has length 1; 2 is required




3
Axel Mendoza
On 9/20/15, 4:27 PM

When you need to override a method, your method signature (it's better) have to match with the original method. For the account.invoice onchange_partner_id you need to define it like:

 @api.multi
def onchange_partner_id(self, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
    #payment_method = res.partner.payment_method
    #payment_method = ['test','TEST!']
    return super(account_invoice, 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)

Thanks, using the same method signature worked fine but I got crazy, restarting the odoo service and upgading the module was not enough and a full reboot was actually needed.

E.M.
on 9/21/15, 5:49 PM
3
Anil R. Kesariya
On 9/21/15, 5:57 AM

Hi EM,

This might be helps you.

 @api.v7
def onchange_partner_id((self, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
#payment_method = res.partner.payment_method
#payment_method = ['test','TEST!']
return super(account_invoice, self).onchange_partner_id((type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False)

Cheer!

Anil.


It's fully work !!! + 1

Ankit H Gandhi(AHG)
on 11/26/15, 8:05 AM
3
Pawan
On 9/21/15, 3:18 AM

You should use the same definition of the function as defined in base:

@api.multi
def onchange_partner_id(self, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):

and then call its super:

res = super(account_invoice, 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)

then at last, append your result to the result "res" that came from super call:

res['value'].update({'payment_method':YOUR_DATA_FROM_res_partner_PAYMENT_METHOD_FIELD})
return res

Hope this will help...

Thanks Pawan, your point of extending return value is useful.

E.M.
on 9/21/15, 5:51 PM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 9/20/15, 1:51 PM
Seen: 1564 times
Last updated: 11/26/15, 8:05 AM