如何计算odoo 13销售订单行上的字段?

问题描述

我正在尝试计算销售订单行上的折扣字段,该方法在odoo 12中效果很好,但是在odoo 13中,每次尝试添加行时都会出现此错误

sale.order.line(,)。discount_mount

这是我所做的

class discount_cycle(models.Model):
_inherit = 'sale.order.line'

discount_mount = fields.Float(string="",required=False,compute='discount_calculation')


@api.depends('product_id','discount','price_subtotal')
def discount_calculation(self):
    for rec in self:
        if rec.discount:
            if rec.product_uom_qty > 1:
                rec.discount_mount = ((rec.price_unit * rec.product_uom_qty) * (rec.discount / 100))
            else:
                rec.discount_mount = (rec.price_unit * (rec.discount / 100))
        pass

请注意,这是odoo V 12中的@ api.one,那么我该如何解决此问题,以及在这种情况下是什么替代@ api.one

解决方法

在odoo V13中,您必须将值分配给计算字段,而不是pass,而需要添加else语句并分配默认值

     else:
             self.discount_mount = 0.0

我知道如果我们没有折扣,那么该字段应该为0.0,但是odoo希望您这样做

,

无论如何,您都需要为非存储的计算字段分配一个值,即使它是一个伪造的字段,如果在计算方法期间未分配该值,则计算的存储字段将保留其先前的值,因此请不要依赖任何期望的默认值。

api.one装饰器已删除,现在默认情况下为多记录。您只需从代码中删除装饰器,然后在self上循环(在示例中已完成)。

如果使用其他字段的值,则应使用depends()指定这些字段。

您需要将product_idprice_subtotal替换为price_unitproduct_uom_qty

discount0.0时,discount_mount也应为0.0,在表达式中,您将折扣除以100,然后进行乘法运算。如果discount的值为0.0,这将不成问题,表达式将被评估为0.0,并且discount_mount字段将被设置为{{1 }},这意味着您可以删除0.0表达式:

if

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...