从hr_payslip类扩展onchange_employee_id方法

时间:2017-03-22 03:43:26

标签: python odoo-8

如何从hr_payslip类扩展onchange_employee_id方法,我尝试过这段代码:

import openerp
from openerp.osv import fields, osv
from openerp import api, fields, models, _, SUPERUSER_ID

class hr_payslip(models.Model):
    _inherit = 'hr.payslip'


    @api.onchange('employee_id')
    def onchange_employee_id(self, date_from, date_to, employee_id=False, contract_id=False):
      *#my logic here* 
       return {}

我得到了这样的错误:

    Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 544, in _handle_exception
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 581, in dispatch
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 317, in _call_function
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\service\model.py", line 118, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 314, in checked_call
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 810, in __call__
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 410, in response_wrap
  File "C:\Program Files (x86)\Odoo 8.0\server\openerp\addons\web\controllers\main.py", line 944, in call_kw
  File "C:\Program Files (x86)\Odoo 8.0\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\api.py", line 268, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\api.py", line 399, in old_api
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\models.py", line 5989, in onchange
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\models.py", line 5881, in _onchange_eval
TypeError: onchange_employee_id() takes at most 5 arguments (8 given)

和iam使用odoo v8,谢谢

2 个答案:

答案 0 :(得分:1)

你不需要使用cr,uid,context作为函数的参数。在新的odoo api方法中,你可以使用装饰器来调用onchange。因此,您应该删除 @ api.model 或使用 @ api.onchange('employee_id')。如果你正在使用装饰器,那么你应该删除不需要的 cr,uid,id,context ,我们可以访问 self 这些形式。

如果你需要扩展onchange方法,一个选项是在继承模型'hr.payslip'中重新定义函数并定义一个函数,如下所示

@api.onchange('employee_id')
def onchange_employee_id(self):#gave extra arguments if any
      *logic*
      return {}

答案 1 :(得分:1)

您必须从原始功能

复制剪切def线
def onchange_employee_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None):
        res=super(hr_payslip, self).onchange_employee_id(self, cr, uid, ids, date_from, date_to, employee_id=False, contract_id=False, context=None)

        # Add your code here
        return res