如何覆盖自定义模块的ORM方法取消链接?

时间:2022-07-19 20:28:39

I want to overwrite the unlink method of stock.move model. The reason is that I want to remove an OSV exception which warns about a forbidden action, and replace it with other message and other condition.

我想覆盖stock.move模型的unlink方法。原因是我想删除一个OSV异常,该异常警告禁止操作,并将其替换为其他消息和其他条件。

This is the original code:

这是原始代码:

def unlink(self, cr, uid, ids, context=None):
    context = context or {}
    for move in self.browse(cr, uid, ids, context=context):
        if move.state not in ('draft', 'cancel'):
            raise osv.except_osv(_('User Error!'), _('You can only delete draft moves.'))
    return super(stock_move, self).unlink(cr, uid, ids, context=context)

I have just realized that removing that message is complexer than I thought. This is my current code, which is checking my condition, but then checks the original one I want to avoid:

我刚刚意识到删除该消息比我想象的要复杂。这是我当前的代码,它检查我的状况,但然后检查我想避免的原始代码:

class StockMove(models.Model):
    _inherit = 'stock.move'

    @api.multi
    def unlink(self):
        for move in self:
            if move.lot_id and move.lot_id.any_unit_sold is True:
                raise Warning(_('You can only delete unsold moves.'))
        return super(StockMove, self).unlink()

If I turn the last line (super) into self.unlink(), I get a maximum recursion depth exceeded error.

如果我将最后一行(超级)转换为self.unlink(),我会得到一个超出最大递归深度的错误。

How can I manage my purpose from my custom module?

如何从自定义模块管理我的目的?

1 个解决方案

#1


1  

Not using a super() call can have unexpected behaviour. You could call models.Model.unlink(), but that will skip all unlink() extensions for stock.move by other modules (even Odoo S.A. apps/modules). In your case it would be:

不使用super()调用可能会出现意外行为。你可以调用models.Model.unlink(),但是这将跳过其他模块(甚至是Odoo S.A. apps / modules)的stock.move的所有unlink()扩展。在你的情况下,它将是:

class StockMove(models.Model):
    _inherit = 'stock.move'

    @api.multi
    def unlink(self):
        for move in self:
            if move.lot_id and move.lot_id.any_unit_sold is True:
                raise Warning(_('You can only delete unsold moves.'))
        return models.Model.unlink(self)

Another possibility would be a monkey patch on the original code.

另一种可能性是原始代码上的猴子补丁。

#1


1  

Not using a super() call can have unexpected behaviour. You could call models.Model.unlink(), but that will skip all unlink() extensions for stock.move by other modules (even Odoo S.A. apps/modules). In your case it would be:

不使用super()调用可能会出现意外行为。你可以调用models.Model.unlink(),但是这将跳过其他模块(甚至是Odoo S.A. apps / modules)的stock.move的所有unlink()扩展。在你的情况下,它将是:

class StockMove(models.Model):
    _inherit = 'stock.move'

    @api.multi
    def unlink(self):
        for move in self:
            if move.lot_id and move.lot_id.any_unit_sold is True:
                raise Warning(_('You can only delete unsold moves.'))
        return models.Model.unlink(self)

Another possibility would be a monkey patch on the original code.

另一种可能性是原始代码上的猴子补丁。