Odoo - 用新api覆盖旧的api方法?

时间:2015-04-30 12:28:07

标签: python odoo python-decorators

我试图覆盖compute_refund方法,但我收到了错误消息。我使用错误的装饰器吗? (已尝试,modelmultione,但其中每一个都会出错。 例如,这样做:

@api.model
def compute_refund(mode='refund'):
    inv_ids = self.env.context.get('active_ids', [])
    for invoice in self.env['account.invoice'].browse(inv_ids):
        if invoice.employee_invoice:
            if mode == 'refund':
                raise Warning(_("This Refund Method is not supported for employee invoices"))
            if mode == 'cancel':
                #Unrelate such invoice from insurance.commission.payment record
                invoice.commission_payment_id = None
                for line in invoice.invoice_line:
                    line.insurance_commission_ids.write({'state': 'confirm'})
                    line.insurance_commission_ids = None
    return super(account_invoice_refund, self).compute_refund(mode=mode)

但是在尝试运行方法时它只是给出了这个错误:

Traceback (most recent call last):
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 530, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 567, in dispatch
    result = self._call_function(**self.params)
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 303, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/openerp/service/model.py", line 113, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 300, in checked_call
    return self.endpoint(*a, **kw)
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 796, in __call__
    return self.method(*args, **kw)
  File "/home/oerp/openerp80/odoo/openerp/http.py", line 396, in response_wrap
    response = f(*args, **kw)
  File "/home/oerp/openerp80/odoo/addons/web/controllers/main.py", line 939, in call_button
    action = self._call_kw(model, method, args, {})
  File "/home/oerp/openerp80/odoo/addons/web/controllers/main.py", line 927, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/addons/account/wizard/account_invoice_refund.py", line 231, in invoice_refund
    return self.compute_refund(cr, uid, ids, data_refund, context=context)
  File "/home/oerp/openerp80/odoo/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/openerp/api.py", line 335, in old_api
    recs = self.browse(cr, uid, [], context)
  File "/home/oerp/openerp80/odoo/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/oerp/openerp80/odoo/openerp/models.py", line 5203, in browse
    return self._browse(Environment(cr, uid, context or {}), ids)
  File "/home/oerp/openerp80/odoo/openerp/api.py", line 709, in __new__
    self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context))
TypeError: cannot convert dictionary update sequence element #0 to a sequence

3 个答案:

答案 0 :(得分:1)

即使问题已经回答,我也想说将@api.model更改为@api.multi也有助于返回视图。我搜索了几个小时,这终于解决了我的问题! 这是我的工作代码示例:

@api.multi
def action_button_return_view(self):
    view_id = self.env.ref('external_id').id
    return {
        'name': 'Name',
        'view_type': 'form',
        'view_mode': 'tree',
        'view_id': view_id,
        'res.model': 'your_model',
        'type': 'ir.actions.act_window',
        'target': 'current'
    }

答案 1 :(得分:0)

我的想法太深了。我犯的错误是我在定义方法时忘记添加self参数。并且需要multi装饰器。

答案 2 :(得分:0)

我还不能投票,因为我的标点符号,但是答案1适用于我,我只是想赞美它也会发生当你用@ api.multi装饰一个带有新api的按钮时它会失败这个错误,我希望它能帮助别人知道它是这个错误的解决方案。

用@ api.multi装饰 和 使用self将方法附加到类。

@api.multi
def your_method(self):
    -...- Do your stuff.