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

Odoo表单视图进度条:如何显示整数格式(如20/50)而非百分比

Odoo表单进度条显示整数数值(如20/50)的实现方案

优先方案:扩展内置progressbar Widget

通过继承Odoo内置进度条模板,修改显示内容为「当前值/总值」格式,无需开发全新widget,仅需简单的模板继承和视图配置:

步骤1:创建模板继承文件

在自定义模块的views目录下新建progressbar_custom_templates.xml,内容如下:

<odoo>
    <template id="custom_progressbar_template" inherit_id="web.progressbar">
        <!-- 替换原百分比显示为当前值/总值 -->
        <xpath expr="//span[hasclass('o_progressbar_value')]" position="replace">
            <t t-if="widget.options.current_field and widget.options.total_field">
                <span class="o_progressbar_value">
                    <t t-esc="widget.record.data[widget.options.current_field]"/>/<t t-esc="widget.record.data[widget.options.total_field]"/>
                </span>
            </t>
            <!-- 无配置时保留原百分比显示 -->
            <t t-else="">
                <span class="o_progressbar_value" t-esc="widget.displayValue"/>%
            </t>
        </xpath>
    </template>
</odoo>

步骤2:配置模型计算字段

确保模型中存在current_value(当前进度值)和total_value(进度总值)字段,同时添加计算字段用于进度条的百分比计算:

from odoo import models, fields, api

class YourModel(models.Model):
    _name = 'your.model.name'
    _description = 'Your Model'

    current_value = fields.Integer(string='当前值')
    total_value = fields.Integer(string='总值')
    progress_percent = fields.Float(compute='_compute_progress_percent', string='进度百分比')

    @api.depends('current_value', 'total_value')
    def _compute_progress_percent(self):
        for rec in self:
            if rec.total_value > 0:
                rec.progress_percent = (rec.current_value / rec.total_value) * 100
            else:
                rec.progress_percent = 0.0

步骤3:表单视图配置

在表单视图中使用progressbar widget,并通过options参数传入当前值和总值的字段名:

<field name="progress_percent" widget="progressbar" options="{'current_field': 'current_value', 'total_field': 'total_value'}"/>

简易替代方案:计算字段+进度条并列显示

如果不想修改模板,可直接在视图中同时显示进度文本和进度条,实现类似效果:

步骤1:添加进度文本计算字段

progress_text = fields.Char(compute='_compute_progress_text', string='进度')

@api.depends('current_value', 'total_value')
def _compute_progress_text(self):
    for rec in self:
        rec.progress_text = f"{rec.current_value}/{rec.total_value}" if rec.total_value else "0/0"

步骤2:视图中布局显示

<div class="o_row oe_center">
    <field name="progress_text" class="oe_inline" style="margin-right: 10px;"/>
    <field name="progress_percent" widget="progressbar" class="oe_inline" style="width: 250px;"/>
</div>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 18:31:10