Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算odoo 13销售订单行上的字段?_Python_Python 3.x_Odoo_Decorator_Odoo 13 - Fatal编程技术网

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

Python 如何计算odoo 13销售订单行上的字段?,python,python-3.x,odoo,decorator,odoo-13,Python,Python 3.x,Odoo,Decorator,Odoo 13,我试图计算销售订单行上的折扣字段,该方法在odoo 12中运行良好,但在odoo 13中,我每次尝试添加行时都会出现此错误 销售.订单.行(,).折扣 这就是我所做的事情 class discount_cycle(models.Model): _inherit = 'sale.order.line' discount_mount = fields.Float(string="", required=False , compute='discount_calculation

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

销售.订单.行(,).折扣

这就是我所做的事情

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中,您必须将值分配到计算字段,而不是传递,您需要添加
else
语句并分配默认值

     else:
             self.discount_mount = 0.0

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

您需要在任何情况下为未存储的计算字段分配一个值,即使它是错误的,如果在计算方法期间未分配,计算存储字段将保留其以前的值,因此,不要依赖任何预期的默认值

api.one
decorator被删除,现在默认为多记录。您只需从代码中删除decorator并循环
self
(在您的示例中已经这样做了)

如果它使用其他字段的值,则应使用指定这些字段

您需要将
产品标识
价格小计
替换为
价格单位
产品数量

discount
0.0
时,
discount\u mount
也应为
0.0
,在表达式中,您将折扣除以
100
,然后进行乘法运算。如果
折扣
的值为
0.0
,则不会出现问题,表达式将被计算为
0.0
,并且
折扣_mount
字段将被设置为
0.0
,这意味着如果
表达式:

if rec.discount: