You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Odoo在线版中让浮点数字不显示小数位

Odoo在线版实现销售订单行数量仅允许输入整数

针对你在Odoo在线版中遇到的sales.order.line里quantity(实际对应字段为product_uom_qty)浮点型字段无法限制为整数的问题,以下是适配在线版的可行方案:

问题原因说明

  • widget="integer"仅对原生integer类型字段生效,无法作用于float字段
  • 直接设置digits="[12,0]"无效,因为在线版无法修改核心模型的字段定义,必须通过继承扩展实现

解决方案(分模型验证+视图控制两步)

1. 模型层添加整数验证约束

通过继承sale.order.line模型,给数量字段添加强制整数的验证规则,确保后端数据合规:

from odoo import models, api, exceptions

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

    @api.constrains('product_uom_qty')
    def _enforce_integer_quantity(self):
        for line in self:
            # 检查数量是否为整数,空值不做限制(可根据需求调整)
            if line.product_uom_qty is not None and not line.product_uom_qty.is_integer():
                raise exceptions.ValidationError("销售订单行数量必须为整数!")

2. 视图层调整显示精度

继承销售订单表单视图,修改数量字段的显示配置,让前端输入时默认不显示小数位:

<odoo>
    <record id="custom_sale_order_form" model="ir.ui.view">
        <field name="name">sale.order.form.integer.quantity</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <!-- 定位到订单行的数量字段 -->
            <field name="product_uom_qty" position="attributes">
                <!-- 设置小数精度为0,强制保存输入值 -->
                <attribute name="options">{"decimal_precision": 0}</attribute>
                <attribute name="force_save">1</attribute>
            </field>
        </field>
    </record>
</odoo>

在线版部署方式

将上述代码打包为自定义Odoo模块(需包含__manifest__.py、模型文件、视图文件),通过Odoo在线版的「应用」-「上传模块」功能安装即可。

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:45:15