Search code examples
pythonpython-3.xodooodoo-13

Is Python 3 continue loop statement a problem in compute methods in Odoo 13?


I am migrating a module to version 13.0, which uses continue in a loop inside a compute method, and an error was driving me crazy for a while.

I simplified the code to the minimum until I had this kind of nosense:

@api.depends('move_lines', 'move_lines.price_subtotal')
def _compute_subtotal(self):
    for picking in self:
        if picking.picking_type_id.code not in ['incoming', 'outgoing']:
            continue
        picking.update({
            'amount_untaxed': 3.0,
        })

But I was still receiving the error, which by the way was this (and only was shown when creating new pickings):

stock.picking(<NewId 0x7f5404ba5eb8>,).amount_untaxed

So I realised that the problem was the continue statement, if I removed it, it worked. And I tried to use continue in several loops of other compute methods of standard Odoo modules, with same result.

Until now, if you did not assign a value for a field in a computed method, it automatically took False, so continue was not a problem.

Does anyone experience this problem with continue too?


Solution

  • It is required to set value for each record set. If we use continue and don't set value for that specific recordset, will get issue as you mentioned.

    Try with following code:

    @api.depends('move_lines', 'move_lines.price_subtotal')
    def _compute_subtotal(self):
        for picking in self:
            amount_untaxed = 0.0
            if picking.picking_type_id.code == 'internal':
                amount_untaxed = 3.0
            picking.update({
                'amount_untaxed': amount_untaxed,
            })
    

    Continue will work if we do code something like:

    @api.depends('move_lines', 'move_lines.price_subtotal')
    def _compute_subtotal(self):
        for picking in self:
            picking.update({
                'amount_untaxed': 0.0,
            })
            if picking.picking_type_id.code not in ['incoming', 'outgoing']:
                continue
            picking.update({
                'amount_untaxed': 3.0,
            })