怎么会呢@api.约束在奥多8工作?

2024-06-09 05:00:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在奥多8中施加一个约束。我已经阅读了它的解释并遵循了示例:

Decorates a constraint checker. Each argument must be a field name used in the check. Invoked on the records on which one of the named fields has been modified. (from https://www.odoo.com/documentation/8.0/reference/orm.html)

This decorator will ensure that decorated function will be called on create, write, unlink operation. If a constraint is met the function should raise a openerp.exceptions.Warning with appropriate message. (from http://odoo-new-api-guide-line.readthedocs.io/en/latest/decorator.html)

但对我来说,这根本不起作用。我为依赖于state字段的stock.picking模型设置了一个约束(一开始它依赖于picking_type_idstate和{}字段,但为了简化问题,我更改了它):

@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.')
         )

问题是,当我创建一个新的拾取时,约束被调用,并且不再调用次数。如果我标记为“待办”、“确认”或“转移”领料,则其状态将更改,但不再调用约束。在

我错过了什么吗?谁能帮帮我吗?在


Tags: theinselfapiidmoveoncheck
1条回答
网友
1楼 · 发布于 2024-06-09 05:00:39

看起来问题可能与它是一个老式的计算字段有关。使用新样式的api简单地重写state字段和stock.picking模型的_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)

这个解决办法对我有效。在

相关问题 更多 >