创建继承覆盖odoo

时间:2018-07-27 19:16:35

标签: python odoo odoo-10

我正在尝试使用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

但是错误是,现在仅执行我添加的新代码,并且不执行原始方法的代码,我不知道如何编辑该方法而不是完全取消该方法。

1 个答案:

答案 0 :(得分:0)

您的方法还不错;-)

origin方法使用api.multi作为修饰符,请勿在扩展名中进行更改。 api.model将使它成为非实例/记录集方法,因此超级调用将使用一个空的记录集完成,这将导致未进行任何验证。

加:您的扩展程序仅在将验证一张发票的情况下起作用。但是,一张以上的发票会怎样?由于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