
本教程详细阐述了如何在odoo产品变体(`product.product`)列表中添加一个基于产品模板(`product.template`)自定义字段的搜索功能。通过定义关联字段并正确使用`filter_domain`属性,我们解决了常见的搜索视图配置错误,确保用户能够高效地根据模板层面的信息筛选产品变体,提升系统可用性。
在Odoo的实际应用中,我们经常需要在产品变体(product.product)的列表视图中,根据产品模板(product.template)上定义的字段进行搜索。例如,如果产品模板上有一个自定义的“型号”(model_number)字段,我们可能希望在产品变体界面直接通过型号来查找对应的变体。本教程将引导您完成这一功能的实现,并纠正一个常见的配置错误。
首先,我们需要在product.template模型中添加一个自定义字段。这个字段将存储我们希望在产品变体界面进行搜索的信息。
# 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)在这个例子中,我们添加了一个名为model_number的字符型字段,用于存储产品的型号。
由于model_number字段定义在product.template上,而我们希望在product.product的搜索视图中进行搜索,因此我们需要在product.product模型上创建一个关联字段,将其链接到product_tmpl_id上的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以提高搜索性能这里,model_number_search是一个related字段,它会自动从关联的产品模板中获取model_number的值。readonly=True表示该字段不可编辑,store=True则非常重要,它会将关联字段的值存储在product.product的数据库表中,从而允许对该字段进行索引,显著提高搜索查询的性能。
接下来,我们需要修改product.product的搜索视图,添加一个用于搜索model_number_search字段的输入框。
初始尝试及常见错误:
许多开发者可能会尝试使用domain属性来定义搜索逻辑,如下所示:
<!-- your_module/views/product_product_views.xml -->
<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">
<!-- 错误示例:使用 domain 属性 -->
<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:[('product_tmpl_id.model_number', 'ilike', self)])。
理解错误原因:
这个错误的核心在于对domain和filter_domain属性的混淆以及对搜索视图中字段引用的不当。
要正确实现搜索功能,我们需要将domain属性替换为filter_domain,并且在filter_domain中直接引用product.product模型上的关联字段model_number_search。
<!-- your_module/views/product_product_views.xml -->
<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)]"/>
</xpath>
</field>
</record>在这个修正后的代码中:
为了清晰起见,以下是所有相关代码的完整整合:
1. 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)2. 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)3. your_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">
<field name="model_number_search" string="型号"
filter_domain="[('model_number_search', 'ilike', self)]"/>
</xpath>
</field>
</record>
</odoo>4. your_module/__init__.py
from . import models
5. your_module/__manifest__.py
确保在depends中包含product模块,并在data中包含XML视图文件。
{
'name': 'Custom Product Search',
'version': '1.0',
'category': 'Sales',
'summary': 'Adds custom search field to product variants',
'depends': ['product'],
'data': [
'views/product_product_views.xml',
],
'installable': True,
'application': False,
'auto_install': False,
}完成上述代码的添加和模块的升级后,您将在产品变体列表的搜索栏中看到一个新的“型号”搜索字段,并且可以根据产品模板上的型号信息进行有效搜索。
通过本教程,我们学习了如何在Odoo产品变体界面中,为产品模板上的自定义字段添加有效的搜索功能。关键在于在产品变体模型中创建store=True的关联字段,并在搜索视图中使用filter_domain属性来定义搜索逻辑。理解filter_domain与domain的区别,是避免常见错误并构建高效Odoo自定义功能的关键。遵循这些步骤和最佳实践,您将能够为您的Odoo系统添加更多灵活和用户友好的搜索功能。
以上就是Odoo产品变体界面添加产品模板字段搜索功能指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号