Odoo销售订单表单按产品自定义属性过滤订单行问题
问题描述
我在product.template模型中继承扩展了名为emptygoods的布尔属性,希望在销售订单表单中过滤order_line,仅显示对应产品emptygoods为False的订单行。尝试通过XPath为order_line字段设置domain属性[('product_id.emptygoods', '=', True)](实际需求应为False)但未生效,多次调整表达式均无效果。
现有代码
product.py
from odoo import models, fields, api class product_template_inherit(models.Model): _inherit = 'product.template' emptygoods = fields.Boolean("Is empty goods")
XML代码
<xpath expr="//field[@name='order_line']" position="attributes"> <attribute name="domain">[('product_id.emptygoods', '=', True)]</attribute> </xpath>
问题分析与解决办法
字段关联错误:销售订单行(
sale.order.line)的product_id关联的是product.product模型,而非直接关联product.template。直接使用product_id.emptygoods无法访问到模板上的字段,必须通过product_id.product_tmpl_id关联到产品模板。修正Domain表达式:将Domain调整为正确的关联路径,同时匹配需求的
False值:
<xpath expr="//field[@name='order_line']" position="attributes"> <attribute name="domain">[('product_id.product_tmpl_id.emptygoods', '=', False)]</attribute> </xpath>
- 额外验证步骤:
- 确保模块已完整升级,
emptygoods字段已同步至数据库 - 检查销售订单行的产品是否正确关联了对应模板,且
emptygoods属性设置无误 - 如果需要在添加订单行时就过滤产品,可同步调整弹窗内产品选择框的Domain:
<xpath expr="//form[@string='Add a product']//field[@name='product_id']" position="attributes"> <attribute name="domain">[('product_tmpl_id.emptygoods', '=', False)]</attribute> </xpath>
内容的提问来源于stack exchange,提问作者Jesse
相关产品推荐
相关产品推荐

