【问题标题】:Odoo 10 - Overriding unlink methodOdoo 10 - 覆盖取消链接方法
【发布时间】:2017-08-19 14:37:53
【问题描述】:

我正在覆盖 account.invoice 中的取消链接方法以允许删除最后一张发票。

这是我的代码:

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

    @api.multi
    def unlink(self):
        for invoice in self:
            if invoice.state not in ('draft', 'cancel'):
                raise UserError(('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
            elif invoice.move_name:
                if invoice.journal_id.sequence_id:
                    sequence_id = invoice.journal_id.sequence_id
                    last_assigned_number = sequence_id.next_number_do_not_increase() - 1
                    last_assigned_number_text = sequence_id.get_next_char(last_assigned_number)
                    if last_assigned_number_text == invoice.move_name:
                        invoice.journal_id.sequence_id.write({'number_next': last_assigned_number})
                    else:
                        raise UserError(('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
        return super(AccountInvoice, self).unlink()

到目前为止一切顺利,

我的具体问题在最后一行,当我运行此代码流时,因此在此 ROUTINE 中没有引发 UserErrors,但随后它运行 super(AccountInvoice, self).unlink() 并执行旧代码形式 account_invoice .py:

@api.multi
def unlink(self):
    for invoice in self:
        if invoice.state not in ('draft', 'cancel'):
            raise UserError(_('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
        elif invoice.move_name:
            raise UserError(_('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
    return super(AccountInvoice, self).unlink()

这会引发错误,我应该如何重写这个 unlink 方法以免发生这种情况?

【问题讨论】:

  • 您能在问题中附上错误吗?

标签: odoo odoo-10


【解决方案1】:

不知道这个作品有没有试一试。

自称发票的上级。

           super(invoice.AccountInvoice, self).unlink()

别忘了先导入发票。

【讨论】:

  • 不建议也不需要扩展类。 OP 正在使用正确的继承方法。
  • 我终于做到了:super(invoice.AccountInvoice,self).unlink(),它强制使用继承的 -original- 方法中的 super。我保留models.Model。我怀疑你的答案也可能有用。
  • 我的第一个想法是做你所做的。我正在使用我的手机没有我的电脑来尝试它。但又是一个非常有用的问题
【解决方案2】:

如果您覆盖原始方法而不是扩展(添加到)它,那么您只需要避免调用super

    # replace this line to prevent the original method from running
    # return super(AccountInvoice, self).unlink()

    # this will unlink (delete) all records in the recordset without
    # calling the original method (which is what super does)
    return self.unlink()

【讨论】:

  • 但是我还是想运行原方法的父方法。
  • @usk70 为什么?它将继续根据原来的方法抛出错误。似乎它基本上会使您的自定义方法无用。
  • 注意我说的是:原始方法的父方法,也就是我们覆盖方法的祖父方法。
  • @usk70 祖父方法在做什么?可能需要将其直接合并到您的方法中。
【解决方案3】:

导入首选的覆盖方法

from odoo.addons.account.models.account_invoice import AccountInvoice as BaseAccountInvoice

然后调用它

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.multi
    def unlink(self):
        return super(BaseAccountInvoice,self).unlink()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多