如何显示定义了十进制精度的字段? Odoo 14

问题描述

在“purchase.order.line”模型中,我想更改“unit_price”和“price_subtotal”字段的小数精度

这些字段的精度应为 4 位并显示在采购订单行中。

这是我的代码

class PurchaSEOrderLine(models.Model):
    _inherit = "purchase.order.line"
    price_unit = fields.Float(string='Unit Price',required=True,digits=(16,4))
    price_subtotal = fields.Monetary(compute='_compute_amount',string='Subtotal',store=True,digits = (16,4))

我只得到了 4 个数字的“price_unit”字段,但“price_subtotal”没有任何变化。是因为计算域吗?

请问我该如何解决

谢谢。

解决方法

发生这种情况是因为它是一个货币字段,它从货币而不是数字参数中获取十进制精度。与货币相关的字段可以通过使用currency_field 作为Monetary 字段的属性来设置,默认为currency_id。

,

如果您只想要 price_subtotal 字段的精度,那么您必须将其声明为 Float 而不是 Monetary ,您将丢失某些高级内容[例如显示货币]。

但是,如果您想更改小计中的整体精度,您可以更改货币的精度。