创建继承覆盖odoo

create inherit override odoo

我正在尝试使用 odoo 10 执行覆盖,事实是我想向 odoo 的现有方法添加功能,但我不知道该怎么做,我已经添加了我的高级功能,但是行为不当

odoo验证底部的原始方法:

@api.multi
def action_invoice_open(self):
    # lots of duplicate calls to action_invoice_open, so we remove those already open
    to_open_invoices = self.filtered(lambda inv: inv.state != 'open')
    if to_open_invoices.filtered(lambda inv: inv.state not in ['proforma2', 'draft']):
        raise UserError(_("Invoice must be in draft or Pro-forma state in order to validate it."))
    to_open_invoices.action_date_assign()
    to_open_invoices.action_move_create()
    return to_open_invoices.invoice_validate()

我想将这段代码添加到这个函数中:

print('enter')
        Replique = self.env['dues.replique'] 
            new = Replique.create({
                    're_customer': self.partner_id.id,
                    'amount_invoice':self.amount_total,
                    'amount_total':self.t_invoice_amount,
                    'n_invoice' : self.number,
                })

我是这样做的:

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

    @api.model
    def action_invoice_open(self):
        print('enter')
        Replique = self.env['dues.replique'] 
            new = Replique.create({
                    're_customer': self.partner_id.id,
                    'amount_invoice':self.amount_total,
                    'amount_total':self.t_invoice_amount,
                    'n_invoice' : self.number,
                })
        campus_write = super(AddFields,self).action_invoice_open()
        return campus_write

但是报错是现在只执行了我添加的新代码,没有执行原来方法的代码,不知如何编辑方法而不是完全取消。

你的方法还不错 ;-)

origin 方法使用 api.multi 作为装饰器,不要用您的扩展程序更改它。 api.model 将使它成为一个非 instance/recordset 方法,因此超级调用将使用空记录集完成,这将导致没有任何验证。

另外:您的扩展只有在验证一张发票时才有效。但是,多于一张发票会怎样?它会引发 "expected singleton" 错误,因为 self 不会是 Singleton.

所以只需将其更改为循环即可:

@api.multi  # don't change it to another decorator
def action_invoice_open(self):
    Replique = self.env['dues.replique']
    for invoice in self:
        Replique.create({
            're_customer': invoice.partner_id.id,
            'amount_invoice':invoice.amount_total,
            'amount_total':invoice.t_invoice_amount,
            'n_invoice' : invoice.number,
        })
    result = super(AddFields, self).action_invoice_open()
    return result