python - 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
?
解决方案
需要为每个记录集设置值。如果我们使用 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,
})
推荐阅读
- python - pandas DataFrame 的滚动 idxmin/max
- sorting - 使用多列 CGridCtrl 自定义排序
- python - 如何在python中读取多索引数据框
- r - 为什么我完成的 R 直方图没有我指定的 bin 数量?
- matplotlib - 如何从值表中绘制轮廓?
- android - 如何知道用户何时完成移动 RecyclerView 项目?
- graphql - 什么是 GraphQL 上下文中的“完全水合的用户对象”?
- python - UnicodeDecodeError:“utf-8”编解码器无法解码脚本中的字节错误
- java - Java + Resilience4J - 我自己的处理程序的时间限制器?
- javascript - 如何在每个时刻的状态下处理带有动画的滚动事件?