创建继承覆盖od

2024-06-01 01:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用odoo10执行覆盖,事实是我想给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()

我想将此代码添加到此函数:

^{pr2}$

我是这样做的:

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

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


Tags: to方法代码inodooselfapidef
1条回答
网友
1楼 · 发布于 2024-06-01 01:25:36

你的方法还不错;-)

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

相关问题 更多 >