
本文旨在指导用户如何在odoo产品变体(`product.product`)列表中,为产品模板(`product.template`)上的自定义字段添加高效的搜索功能。文章将详细阐述如何定义关联字段、配置搜索视图,并着重强调在使用关联字段作为搜索条件时,应正确利用`filter_domain`属性,以避免常见的领域表达式错误,从而实现精准且用户友好的产品变体搜索体验。
在Odoo的实际应用中,我们经常需要在产品变体(product.product)的视图中,根据其关联产品模板(product.template)上的信息进行筛选和搜索。例如,如果我们在产品模板上定义了一个自定义的“型号”字段,并希望在产品变体的列表中直接通过该型号进行搜索,就需要进行一些特定的配置。本教程将详细介绍如何实现这一功能,并纠正常见的配置错误。
首先,我们需要在product.template模型上添加一个自定义字段,例如model_number。这个字段将用于存储产品的型号信息。
# /your_module/models/product_template.py
from odoo import fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
model_number = fields.Char(string='型号', required=True, help="产品的型号编码")说明:
由于搜索功能是在product.product视图中进行的,但model_number字段定义在product.template上,我们需要在product.product模型上创建一个关联字段,以便能够直接访问product.template上的model_number。
# /your_module/models/product_product.py
from odoo import fields, models
class ProductProduct(models.Model):
_inherit = 'product.product'
# 关联到产品模板的model_number字段
model_number_search = fields.Char(
related='product_tmpl_id.model_number',
string='型号',
readonly=True,
store=True, # 强烈建议设置为True,以提高搜索性能
help="关联产品模板的型号,用于搜索"
)说明:
现在,我们将在product.product的搜索视图中添加一个搜索字段,允许用户通过model_number_search进行搜索。
<!-- /your_module/views/product_views.xml -->
<odoo>
<data>
<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="型号"
filter_domain="[('model_number_search', 'ilike', self)]"
/>
</xpath>
</field>
</record>
</data>
</odoo>说明:
为了更好地理解,以下是包含所有必要部分的完整代码结构:
your_module/__manifest__.py:
{
'name': "Product Variant Model Search",
'version': '1.0',
'category': 'Sales/Product',
'summary': 'Adds model number search to product variants.',
'depends': ['product'],
'data': [
'security/ir.model.access.csv', # 如果有自定义模型,需要权限文件
'views/product_template_views.xml', # 如果需要显示在产品模板视图中
'views/product_product_views.xml',
],
'installable': True,
'application': False,
'auto_install': False,
}your_module/models/product_template.py:
from odoo import fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
model_number = fields.Char(string='型号', required=True, help="产品的型号编码")your_module/models/product_product.py:
from odoo import fields, models
class ProductProduct(models.Model):
_inherit = 'product.product'
model_number_search = fields.Char(
related='product_tmpl_id.model_number',
string='型号',
readonly=True,
store=True,
help="关联产品模板的型号,用于搜索"
)your_module/models/__init__.py:
from . import product_template from . import product_product
your_module/views/product_template_views.xml (可选,用于在产品模板表单中显示字段):
<odoo>
<data>
<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>
</data>
</odoo>your_module/views/product_product_views.xml:
<odoo>
<data>
<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="型号"
filter_domain="[('model_number_search', 'ilike', self)]"
/>
</xpath>
</field>
</record>
</data>
</odoo>通过本文的详细步骤,你现在应该能够成功地在Odoo产品变体列表中,为产品模板上的自定义字段添加搜索功能。关键在于理解并正确使用product.product上的关联字段以及搜索视图中filter_domain属性的机制。遵循这些最佳实践,可以有效地扩展Odoo的功能,提升用户体验和数据检索效率。
以上就是Odoo 产品变体搜索功能扩展:为产品模板自定义字段添加搜索支持的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号