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

Odoo 10向导树视图记录行数统计及按钮显示需求咨询

嗨,针对你在Odoo 10里遇到的这个需求——把向导树视图的记录数显示在表单的“On Hand”按钮上,我整理了一套可行的实现方案,你可以按步骤来操作:

1. 给向导模型添加统计记录数的方法

假设你的向导模型叫stock.quant.wizard(根据实际名称调整),在模型文件里新增一个方法,用来统计和当前表单记录关联的向导条目数量:

from odoo import models, fields, api

class StockQuantWizard(models.TransientModel):
    _name = 'stock.quant.wizard'
    # 这里保留你已有的字段定义...

    @api.model
    def get_associated_count(self, current_record_id):
        # current_record_id是当前表单记录的ID(比如产品ID)
        # 替换成你实际的关联字段查询条件
        return self.search_count([('product_id', '=', current_record_id)])

2. 在表单所属模型中添加计算字段

假设你的表单是产品模型product.product,继承这个模型并添加一个计算字段,用来实时获取向导的记录数:

class ProductProduct(models.Model):
    _inherit = 'product.product'

    quant_wizard_count = fields.Integer(
        string='Wizard Record Count',
        compute='_compute_quant_wizard_count',
        store=False  # 不需要持久化,实时计算
    )

    @api.depends()
    def _compute_quant_wizard_count(self):
        for product in self:
            # 调用向导模型的统计方法
            product.quant_wizard_count = self.env['stock.quant.wizard'].get_associated_count(product.id)

3. 修改表单视图的按钮定义

找到你表单视图中“On Hand”按钮的XML代码,修改string属性,用动态占位符把统计数嵌入按钮文本:

<button 
    name="action_open_quant_wizard" 
    string="On Hand (%(quant_wizard_count)s)" 
    type="object" 
    class="oe_highlight"
/>

这里的action_open_quant_wizard是你原来打开向导的方法名,quant_wizard_count就是刚才新增的计算字段,Odoo会自动替换成实际的数值。

4. 确保向导打开时的记录筛选正确

检查打开向导的方法,确保它能正确筛选出和当前表单记录关联的条目,比如:

def action_open_quant_wizard(self):
    self.ensure_one()
    # 筛选当前产品对应的向导记录
    wizard_records = self.env['stock.quant.wizard'].search([('product_id', '=', self.id)])
    return {
        'name': 'On Hand Quantities',
        'type': 'ir.actions.act_window',
        'res_model': 'stock.quant.wizard',
        'view_mode': 'tree',
        'domain': [('id', 'in', wizard_records.ids)],
        'target': 'new',  # 保持向导的弹窗模式
    }

注意事项

  • 替换模型和字段名:上面的示例用的是假设的模型和字段,你需要根据自己的实际业务调整(比如关联字段不是product_id的话,要改成你实际用的字段)。
  • 权限检查:确保当前用户有读取向导模型记录的权限,否则统计数会显示为0。
  • 测试验证:修改完成后升级模块,刷新页面,就能看到按钮上显示向导内的记录数了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:05:36