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

Odoo 14:如何仅在销售模块修改product_template_id字段的显示名称?

Odoo 14 销售模块仅显示产品内部参考编号的解决方案

核心思路

不全局修改product.product的name_get方法,而是通过上下文标记+局部重写的方式,仅在销售订单的产品选择场景下返回内部参考编号,其他模块保持原有显示逻辑。

具体实现步骤

1. 创建自定义模块(依赖sale模块)

在自定义模块的__manifest__.py中声明依赖:

{
    'name': 'Sale Product Default Code Display',
    'version': '1.0',
    'depends': ['sale'],
    'author': 'Your Name',
    'data': [
        'views/sale_order_views.xml',
    ],
    'installable': True,
    'auto_install': False,
}

2. 重写product.product的name_get方法(仅在销售场景生效)

在模块的models.py中添加以下代码:

from odoo import models, api

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

    @api.multi
    def name_get(self):
        # 仅当上下文包含销售场景标记时,返回内部参考编号
        if self.env.context.get('sale_product_display_default_code'):
            result = []
            for product in self:
                # 无内部参考时 fallback 到产品名称
                display_name = product.default_code or product.name
                result.append((product.id, display_name))
            return result
        # 其他场景调用原生方法
        return super(ProductProduct, self).name_get()

3. 给销售订单的产品字段添加上下文标记

在模块的views/sale_order_views.xml中,修改销售订单行的product_id字段,传入自定义上下文:

<odoo>
    <record id="view_order_form_inherit" model="ir.ui.view">
        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <!-- 修改销售订单行的产品字段上下文 -->
            <field name="order_line" position="attributes">
                <attribute name="context">{'sale_product_display_default_code': True}</attribute>
            </field>
            <!-- 修改添加产品弹窗的上下文,确保弹窗内也显示内部参考 -->
            <button name="%(sale.action_product_product_pricelist)s" position="attributes">
                <attribute name="context">{'sale_product_display_default_code': True}</attribute>
            </button>
        </field>
    </record>
</odoo>

关键说明

  • 上下文标记sale_product_display_default_code仅在销售订单相关视图中传入,确保采购、会计等其他模块的产品显示逻辑不受影响。
  • 代码处理了产品无内部参考的边界情况,避免显示空值。
  • 安装该模块后,销售订单中选择产品时自动显示内部参考编号,其他场景保持原有产品显示规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 00:40:18