How does @ api.constrains work in Odoo 8?

I am trying to apply a restriction in Odoo 8. I read its explanation and followed the examples:

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

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

But in my case, this does not work at all. I made a restriction for the model stock.picking, which depends on the field state(at the beginning it depended on the fields picking_type_id, stateand move_lines, but I changed this to simplify the problem):

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

The problem is that the restriction is called when I create a new collection and not more than once. If I mark what to make, confirm or reschedule a choice, its state changes, but the restriction is no longer called.

Anything I missed? Can someone help me please?

+4
source share
1 answer

, , . state _state_get stock.picking api , , , .

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)

.

+1

Source: https://habr.com/ru/post/1659598/


All Articles