首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Odoo 10 -重写unlink方法

Odoo 10 -重写unlink方法
EN

Stack Overflow用户
提问于 2017-08-19 22:37:54
回答 3查看 5.5K关注 0票数 2

我正在重写account.invoice中的unlink方法,以允许删除最后一次提交的发票。

这是我的代码:

代码语言:javascript
复制
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()

到目前一切尚好,

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

代码语言:javascript
复制
@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方法才不会发生这种情况?

EN

回答 3

Stack Overflow用户

发布于 2017-08-20 21:18:46

不知道这项工作是不是可以试试。

称它为发票本身的超级。

代码语言:javascript
复制
           super(invoice.AccountInvoice, self).unlink()

别忘了先导入发票。

票数 2
EN

Stack Overflow用户

发布于 2017-08-20 13:17:45

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

代码语言:javascript
复制
    # 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()
票数 1
EN

Stack Overflow用户

发布于 2020-06-08 17:43:09

导入首选的覆盖方法

代码语言:javascript
复制
from odoo.addons.account.models.account_invoice import AccountInvoice as BaseAccountInvoice

那就叫它

代码语言:javascript
复制
class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.multi
    def unlink(self):
        return super(BaseAccountInvoice,self).unlink()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45772787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档