How to get models that inherit a given model in odoo

45 Views Asked by At

Odoo's ir.model defines the following field

inherited_model_ids = fields.Many2many('ir.model', compute='_inherited_models', string="Inherited models",
                                           help="The list of models that extends the current model.")

From this definition I would expect to get a list all models that inherit the mail.thread model, for example, but I'm getting an empty recordset

>>> 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()

Maybe I don't understand how this works,.. I would appreciate if anyone could clearify

EDIT

Upon further investigation, _inherited_models seems to be doing something different from what I understood from the description of inherited_model_ids

@api.depends()
def _inherited_models(self):
    self.inherited_model_ids = False
    for model in self:
        parent_names = list(self.env[model.model]._inherits)
        if parent_names:
            model.inherited_model_ids = self.search([('model', 'in', parent_names)])
        else:
            model.inherited_model_ids = False

Instead, it computes the models that the current model extends (it's parents as opposed to it's children), for example

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

Also, it apparently only considers models extended using _inherits

This all seems a bit confusing to me!

However, I was able to get away with this

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()
0

There are 0 best solutions below