
本教程详细介绍了如何在odoo的产品变体(product.product)列表中添加一个基于产品模板(product.template)自定义字段的搜索功能。文章将指导您完成自定义字段的定义、关联字段的创建,并重点阐述在搜索视图中使用filter_domain而非domain的关键区别与正确实践,以避免常见的配置错误,确保搜索功能高效运行。
在Odoo的实际应用中,我们经常需要对产品变体(product.product)进行筛选,但某些关键信息可能存储在其父级产品模板(product.template)模型上。本教程将以“型号”(model_number)字段为例,详细演示如何在产品变体列表中添加一个基于产品模板型号的搜索功能。
首先,我们需要在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的字符型字段,并将其标记为必填项。
为了在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。
接下来,我们需要修改product.product的搜索视图,以包含我们新定义的model_number_search字段。关键在于正确使用filter_domain属性。
错误示例与原因分析:
初学者常犯的错误是在搜索视图的<field>标签中使用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的错误。这是因为:
正确实现:使用 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>在这个正确的配置中:
为了使上述代码生效,您需要将它们组织到一个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>通过本教程,您应该已经掌握了在Odoo产品变体视图中添加基于产品模板字段的搜索功能的正确方法。关键在于:
遵循这些步骤,您将能够为用户提供更灵活、高效的产品搜索体验。
以上就是Odoo产品变体视图中基于产品模板字段实现搜索功能指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号