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

Odoo中获取继承指定模型的子模型:问题与解决方法

Odoo获取继承指定模型的子模型问题解决

问题场景

我尝试通过Odoo的ir.model模型中定义的inherited_model_ids字段,获取所有继承指定模型(如mail.thread)的子模型,但实际调用返回的是空记录集:

>>> self.env['ir.model'].search([('model','=','mail.thread')]).inherited_model_ids
ir.model()
>>> self.env['ir.model'].search([('model','=','res.partner')]).inherited_model_ids
ir.model()

原因分析

查看inherited_model_ids字段的计算方法_inherited_models后发现,这个字段的实际作用是获取当前模型通过_inherits继承的父模型,而非我们想要的子模型。举个例子:

>>> self.env['ir.model'].search([('model','=','res.users')]).inherited_model_ids
ir.model(85,) # 对应res.partner模型

解决方案

最终通过直接查询数据库表的方式实现了需求,代码如下:

self.env.cr.execute(
    """
        SELECT m.model from ir_model_inherit mi 
        JOIN ir_model m ON m.id = mi.model_id 
        WHERE mi.parent_id = %s 
    """,
    (parent_model_id.id,),
)
child_model_ids = self.env.cr.fetchall()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 13:17:09