有人能帮忙吗我试图划分两个浮点字段,但出现错误
AttributeError: 'stock.picking' object has no attribute 'calculate_reserved_per'
这是我的代码:
from odoo import models, fields, api
class PickingTotalQty(models.Model):
_inherit = 'stock.picking'
sum_dmd_qty = fields.Float(compute='calculate_dmd_qty', string='Total Demand Quantity')
sum_reserved_qty = fields.Float(compute='calculate_reserved_qty', string='Total Reserved Quantity')
reserved_qty_per = fields.Float(compute='calculate_reserved_per', string='Reserved Quantity (%)')
def calculate_dmd_qty(self):
for rs in self:
dmdqty = 0
for line in rs.move_lines:
dmdqty += line.product_uom_qty
rs.sum_dmd_qty = dmdqty
def calculate_reserved_qty(self):
for rs in self:
reservedqty = 0
for line in rs.move_lines:
reservedqty += line.reserved_availability
rs.sum_reserved_qty = reservedqty
@api.depends('sum_reserved_qty', 'sum_dmd_qty')
def calculate_reserved_per(self):
for rec in self:
rec.reserved_qty_per = rec.sum_reserved_qty / rec.sum_dmd_qty
如果您已经重新启动服务器,请检查calculate_reserved_per
方法的缩进,并确保rec.sum_dmd_qty
与Zero
不同,然后再进行分割。
您所面临的奇怪行为是两种方法中最后一行缩进的结果:calculate_dmd_qty
和calculate_reserved_qty
,它们忽略除最后一行之外的所有记录。在表单视图中,使用包含一条记录(即当前记录(的记录集调用该方法。
要更正此问题,请在for循环中移动每个方法的最后一行。
sum_dmd_qty
和sum_reserved_qty
是分别依赖于move_lines.product_uom_qty
和move_lines.reserved_availability
的计算字段,应用于指定何时使用@api.depends
重新计算它们的值。
您可以使用会计发票_compute_amount作为参考,并将这三个方法合并为一个计算三个字段值的方法。
示例:
@api.depends("move_lines.product_uom_qty", "move_lines.reserved_availability")
def _calculate_quantities(self):
for picking in self:
dmd_qty = 0
reserved_qty = 0
for move in picking.move_lines:
dmd_qty += move.product_uom_qty
reserved_qty += move.reserved_availability
picking.sum_dmd_qty = dmd_qty
picking.sum_reserved_qty = reserved_qty
if picking.sum_dmd_qty:
picking.reserved_qty_per = picking.sum_reserved_qty / picking.sum_dmd_qty
我没有使用
@api.one
,因为它在Odoo13 中不可用