登录
首页 >  文章 >  python教程

Odoo变体视图搜索功能详解

时间:2025-11-23 15:18:37 391浏览 收藏

下载万磁搜索绿色版

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Odoo变体视图搜索功能实现指南》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

Odoo产品变体视图中基于产品模板字段实现搜索功能指南

本教程详细介绍了如何在Odoo的产品变体(product.product)列表中添加一个基于产品模板(product.template)自定义字段的搜索功能。文章将指导您完成自定义字段的定义、关联字段的创建,并重点阐述在搜索视图中使用filter_domain而非domain的关键区别与正确实践,以避免常见的配置错误,确保搜索功能高效运行。

在Odoo的实际应用中,我们经常需要对产品变体(product.product)进行筛选,但某些关键信息可能存储在其父级产品模板(product.template)模型上。本教程将以“型号”(model_number)字段为例,详细演示如何在产品变体列表中添加一个基于产品模板型号的搜索功能。

1. 定义产品模板上的自定义字段

首先,我们需要在product.template模型上添加一个自定义字段来存储型号信息。这通常通过继承product.template模型来实现。

# custom_module/models/product_template.py
from odoo import fields, models

class ProductTemplateInherit(models.Model):
    _inherit = 'product.template'

    model_number = fields.Char(string='型号', required=True, help="产品的型号信息")

此代码片段在product.template模型上添加了一个名为model_number的字符型字段,并将其标记为必填项。

2. 在产品变体模型上创建关联字段

为了在product.product视图中访问product.template上的model_number字段,我们需要在product.product模型上创建一个关联字段(related field)。这个关联字段将允许我们直接从产品变体记录中获取其对应产品模板的型号。为了优化搜索性能,建议将store=True设置为True,这样该字段的值会被存储在数据库中,从而加快查询速度。

# custom_module/models/product_product.py
from odoo import fields, models

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

    # 关联到产品模板的型号字段,并设置为存储以优化搜索性能
    model_number_search = fields.Char(
        related='product_tmpl_id.model_number',
        string='产品型号',
        readonly=True,
        store=True,
        help="关联到产品模板的型号,用于搜索和筛选"
    )

在这里,model_number_search字段通过product_tmpl_id.model_number关联到产品模板的型号,并设置了store=True。

3. 修改产品变体搜索视图

接下来,我们需要修改product.product的搜索视图,以包含我们新定义的model_number_search字段。关键在于正确使用filter_domain属性。

错误示例与原因分析:

初学者常犯的错误是在搜索视图的标签中使用domain属性来定义搜索逻辑,例如:

<!-- 错误的搜索视图配置示例 -->
<record id="product_product_search_form_view_inherit" model="ir.ui.view">
    <field name="name">product.product_search_form_view_inherit</field>
    <field name="model">product.product</field>
    <field name="inherit_id" ref="product.product_search_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='name']" position="after">
           <field name="model_number_search" string="型号"
                   domain="[('product_tmpl_id.model_number', 'ilike', self)]"/>
        </xpath>
    </field>
</record>

上述配置会导致类似Domain on non-relational field "model_number_search" makes no sense的错误。这是因为:

  • domain属性主要用于限定关联字段(如many2one, many2many)的备选值范围,或者用于视图本身的domain属性来限定视图显示的数据
  • 当domain用于标签时,Odoo期望这个field是一个关系字段,并且domain是用来过滤这个关系字段的可选记录。
  • 对于一个普通的字符型搜索字段,我们希望它能对当前视图的模型(product.product)应用一个全局的过滤条件,而不是过滤model_number_search字段本身的选项(它不是关系字段)。

正确实现:使用 filter_domain

为了实现一个能够对当前视图记录进行过滤的搜索字段,我们应该使用filter_domain属性。filter_domain定义了一个当用户在该搜索字段中输入值时,将应用于当前视图模型的域表达式。self在filter_domain中代表用户在搜索框中输入的值。

<!-- custom_module/views/product_product_views.xml -->
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="product_product_search_form_view_inherit" model="ir.ui.view">
        <field name="name">product.product_search_form_view_inherit</field>
        <field name="model">product.product</field>
        <field name="inherit_id" ref="product.product_search_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='name']" position="after">
               <!-- 使用 filter_domain 来定义搜索逻辑 -->
               <field name="model_number_search" string="型号"
                      filter_domain="[('model_number_search', 'ilike', self)]"
                      help="通过产品型号搜索产品变体"/>
            </xpath>
        </field>
    </record>
</odoo>

在这个正确的配置中:

  • filter_domain="[('model_number_search', 'ilike', self)]" 指示Odoo,当用户在“型号”搜索框中输入文本时,将生成一个domain表达式 [('model_number_search', 'ilike', '用户输入的值')],并将其应用于product.product模型的记录列表。
  • model_number_search是我们之前在product.product模型上定义的关联字段,因此这个domain是有效的。

4. 完整示例代码(模块结构)

为了使上述代码生效,您需要将它们组织到一个Odoo模块中。

custom_module/__manifest__.py:

{
    'name': 'Custom Product Search',
    'version': '1.0',
    'summary': 'Adds custom search fields to product variants',
    'category': 'Sales/Product',
    'author': 'Your Name',
    'depends': ['product'], # 确保依赖于 product 模块
    'data': [
        'security/ir.model.access.csv', # 如果有自定义模型或访问权限
        'views/product_template_views.xml', # 如果需要修改模板视图
        'views/product_product_views.xml',
    ],
    'installable': True,
    'application': False,
    'auto_install': False,
}

custom_module/models/__init__.py:

from . import product_template
from . import product_product

custom_module/models/product_template.py: (如上述)

custom_module/models/product_product.py: (如上述)

custom_module/views/product_product_views.xml: (如上述)

custom_module/views/product_template_views.xml: (可选,如果需要在产品模板表单中显示model_number字段)

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="product_template_form_view_inherit_model_number" model="ir.ui.view">
        <field name="name">product.template.form.view.inherit.model.number</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='default_code']" position="after">
                <field name="model_number"/>
            </xpath>
        </field>
    </record>
</odoo>

5. 注意事项与最佳实践

  • filter_domain vs domain: 这是本教程的核心。请务必理解filter_domain用于在搜索视图中构建应用于当前模型列表的过滤条件,而domain主要用于限定关系字段的可用选项或视图本身的初始数据范围。
  • store=True的重要性: 对于用于搜索或分组的关联字段,将其store属性设置为True可以显著提高查询性能,因为Odoo会将关联字段的值实际存储在数据库中,避免了每次查询时都进行复杂的联表操作。
  • 模块依赖: 确保您的自定义模块依赖于product模块,因为我们继承了product模块中的模型和视图。
  • Odoo版本: 尽管filter_domain是Odoo中相对稳定的特性,但始终建议在不同Odoo版本之间进行测试,以确保兼容性。

总结

通过本教程,您应该已经掌握了在Odoo产品变体视图中添加基于产品模板字段的搜索功能的正确方法。关键在于:

  1. 在product.template上定义自定义字段。
  2. 在product.product上创建指向该自定义字段的关联字段,并设置store=True。
  3. 在搜索视图中,使用filter_domain属性来定义搜索逻辑,而非domain。

遵循这些步骤,您将能够为用户提供更灵活、高效的产品搜索体验。

好了,本文到此结束,带大家了解了《Odoo变体视图搜索功能详解》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>