首页 > 解决方案 > Python 3 continue 循环语句在 Odoo 13 中的计算方法中是否存在问题?

问题描述

我正在将一个模块迁移到版本 13.0,它continue在计算方法内的循环中使用,一个错误让我发疯了一段时间。

我将代码简化到最低限度,直到我有这种废话:

@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,
        })

但是我仍然收到错误,顺便说一下(并且仅在创建新选择时显示):

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

所以我意识到问题出在continue声明上,如果我删除它,它就起作用了。我尝试continue在标准 Odoo 模块的其他计算方法的几个循环中使用,结果相同。

到目前为止,如果您没有为计算方法中的字段分配值,它会自动取False,所以continue不是问题。

有没有人也遇到过这个问题continue

标签: pythonpython-3.xodooodoo-13

解决方案


需要为每个记录集设置值。如果我们使用 continue 并且不为该特定记录集设置值,则会出现您提到的问题。

尝试使用以下代码:

@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 将起作用:

@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,
        })

推荐阅读