【问题标题】:create inherit override odoo创建继承覆盖odoo
【发布时间】:2018-07-27 19:16:35
【问题描述】:

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

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

【问题讨论】:

  • 使用 super(ClassName,self).function_name()。如果你想在主代码之后添加你的代码,那么将“super”放在顶部

标签: python odoo odoo-10


【解决方案1】:

你的方法还不错;-)

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

【讨论】:

  • 非常感谢,我的朋友,这就是解决方案。
猜你喜欢
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2012-11-20
相关资源
最近更新 更多