You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 11社区版domain_filter不生效强制触发JOIN问题求助

原因说明

Odoo ORM存在固有处理逻辑:只要搜索域中出现关联字段.子字段的跨模型点操作语法,ORM就会自动为该关联字段生成LEFT JOIN关联查询,不会因为你添加了关联字段判空条件就跳过JOIN生成。
同时你原有filter_domain中的parent_id判空逻辑属于冗余内容:当parent_id为空时,('parent_id.code', 'ilike', 搜索值)条件本身的运算结果就是False,不需要额外判断父记录是否存在。

解决方案

方案1:简化搜索视图filter_domain配置

直接修改搜索视图中code字段的filter_domain即可,生成的SQL逻辑和你要求的完全一致:

<record id="search_view_subscription" model="ir.ui.view">
    <field name="name">subscription.search</field>
    <field name="model">subscription</field>
    <field name="arch" type="xml">
        <search string="Subscriptions">
            <field name="code" string="Subscription"
                filter_domain="[
                    '|',
                    ('code', 'ilike', self),
                    ('parent_id.code', 'ilike', self)
                ]"
            />
            <!-- 其他过滤器配置保持不变 -->
        </search>
    </field>
</record>

方案2:重写模型_name_search方法(兜底方案)

如果Odoo 11版本存在搜索视图配置不生效的兼容性问题,可以直接重写Subscription模型的全局搜索方法,优先级高于搜索视图配置,且全局场景生效:

class Subscription(models.Model):
    _name = "subscription"
    _rec_name = 'name'

    parent_id = fields.Many2one(
        'subscription',
        string="Derived From",
        index=True,
        readonly=True
    )

    code = fields.Char(
        string="Reference",
        required=True,
        index=True,
        readonly=True,
        copy=False
    )

    # 其他字段定义保持不变

    @api.model
    def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
        args = args or []
        if name:
            args = ['|', 
                    ('code', operator, name), 
                    ('parent_id.code', operator, name)] + args
        return super()._name_search(name, args, operator, limit, name_get_uid)

内容的提问来源于stack exchange,提问作者Wellington Souza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 08:51:01