【问题标题】:How does @api.constrains work in Odoo 8?@api.constrains 如何在 Odoo 8 中工作?
【发布时间】:2016-11-02 10:47:18
【问题描述】:

我正在尝试在 Odoo 8 中应用约束。我已阅读其解释并遵循示例:

装饰一个约束检查器。每个参数必须是一个字段名 在检查中使用。调用其中一个命名的记录 字段已被修改。 (来自https://www.odoo.com/documentation/8.0/reference/orm.html

这个装饰器将确保被装饰的函数会被调用 创建、写入、取消链接操作。如果满足约束,则函数 应该使用适当的消息引发 openerp.exceptions.Warning。 (来自http://odoo-new-api-guide-line.readthedocs.io/en/latest/decorator.html

但在我的情况下它根本不起作用。我为stock.picking 模型做了一个约束,它依赖于state 字段(一开始它依赖于picking_type_idstatemove_lines 字段,但为了简化问题我改变了它):

@api.one
@api.constrains('state')
def _check_lot_in_outgoing_picking(self):
    _logger.info('MY CONSTRAINT IS CALLED')
    if self.picking_type_id.code == 'outgoing' and \
        self.state not in ['draft', 'cancel'] and \
        any(not move.restrict_lot_id for move in self.move_lines):
         raise ValidationError(
             _('The lot is mandatory in outgoing pickings.')
         )

问题是当我创建一个新的拾取时会调用约束并且不再调用。如果我标记为做、确认或转移拣货,它的状态会改变,但不再调用约束。

我有什么想念的吗?谁能帮帮我?

【问题讨论】:

  • 用工作流函数设置约束

标签: python xml python-2.7 odoo-8 odoo


【解决方案1】:

看起来问题可能与它是一个旧式计算字段这一事实有关。只需使用新式 api 覆盖 stock.picking 模型的 state 字段和 _state_get 方法似乎可以解决问题,并且每次状态更改都会调用约束。

class stock_picking(models.Model):
    _inherit = "stock.picking"

    @api.one
    @api.depends('move_lines', 'move_type', 'move_lines.state')
    def _state_get(self):
        self.state = super(stock_picking, self)._state_get(field_name='state', arg=None, context=self._context)[self.id]

    state = fields.Selection(compute=_state_get)

这个解决方法对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多