Python 3 continue循环语句是Odoo 13中计算方法的问题吗?
我正在将一个模块迁移到13.0版,该版本在计算方法内部的循环中使用Python 3 continue循环语句是Odoo 13中计算方法的问题吗?,python,python-3.x,odoo,odoo-13,Python,Python 3.x,Odoo,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',
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(,).amount\u untaxed
所以我意识到问题出在continue
语句上,如果我删除它,它就会起作用。我尝试在标准Odoo模块的其他计算方法的几个循环中使用continue
,得到了相同的结果
到目前为止,如果您没有在计算方法中为字段赋值,则会自动采用False
,因此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,
})
如果我们编写以下代码,继续将起作用:
@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,
})
问题不在于continue语句本身,而是Odoo希望您为计算字段设置值,因为如果您检查
字段的\uuuuu get\uuuuu
的代码
类:
def __get__(self):
....
.....
elif self.compute:
........
........
else:
recs = record if self.recursive or not record.id else record._in_cache_without(self)
try:
# Here it's computing the value for this record
self.compute_value(recs)
except AccessError:
self.compute_value(record)
# after computation they try the get the value from the environment cache
value = env.cache.get(record, self)
由于您没有设置此记录的值,这将引发此错误odoo.exceptions.CacheMiss:(“stock.picking(记录的ID\u)).amount\u untaxed',None)
。
您需要为使用该方法计算的每个计算字段设置一个值。感谢您的帮助,Charif,但是给了Odedra一个有效的答案,因为他速度更快。我们的答案之间的区别是我解释了为什么您会遇到这个问题。他编写的使用continue的解决方案是不好的,因为他对某些记录触发了两次更新查询(如果字段可存储为curse)。谢谢你的投票和令人敬畏的问题,你总是对的,你的解决方案更深入,事实上,我使用了
继续解决方案,但在如果语句中写入了0.0更新,就在继续之前,以避免对某些记录(该字段已存储)进行doble查询。@forvas exaclty,为什么要使用更新?这只是一个使用普通赋值的字段,picking.amount\u untaxed=value
。实际上,我正在用相同的计算方法更新三个字段,但这里我删除了另一个字段以简化问题的代码。但是很高兴知道这个技巧,这个例子的代码应该在编写时计算。
def __get__(self):
....
.....
elif self.compute:
........
........
else:
recs = record if self.recursive or not record.id else record._in_cache_without(self)
try:
# Here it's computing the value for this record
self.compute_value(recs)
except AccessError:
self.compute_value(record)
# after computation they try the get the value from the environment cache
value = env.cache.get(record, self)