Type of model inheritance in odoo

Type of model inheritance in odoo

 

Type Of model Inheritance in odoo:

Inheritance by extension:

from odoo import models, fields, api


class ParentModel(models.Model):

    _name = ‘parent.model’

    

    name = fields.Char()

    age = fields.Integer()

    

    @api.multi

    def do_something(self):

        # some functionality

        pass


class ChildModel(models.Model):

    _name = ‘child.model’

    _inherit = ‘parent.model’

    

    email = fields.Char()

    

    @api.multi

    def do_something(self):

        # overriding parent method

        pass

In this example, the ChildModel inherits from the ParentModel using the _inherit attribute. The child model also adds a new field email and overrides the do_something method of the parent model.


Inheritance by delegation:

from odoo import models, fields, api


class ParentModel(models.Model):

    _name = ‘parent.model’

    

    name = fields.Char()

    age = fields.Integer()

    

    @api.multi

    def do_something(self):

        # some functionality

        pass


class ChildModel(models.Model):

    _name = ‘child.model’

    

    parent_id = fields.Many2one(‘parent.model’)

    email = fields.Char()

    

    @api.multi

    def do_something(self):

        # some functionality delegated to parent model

        self.parent_id.do_something()

        pass

In this example, the ChildModel does not inherit from the ParentModel. Instead, it creates a Many2one field parent_id to delegate some of its functionality to the ParentModel. The child model also adds a new field email and overrides the do_something method to delegate some functionality to the parent model.


Inheritance by prototype:

from odoo import models, fields, api


class ParentModel(models.Model):

    _name = ‘parent.model’

    

    name = fields.Char()

    age = fields.Integer()

    

    @api.multi

    def do_something(self):

        # some functionality

        pass


class ChildModel(models.Model):

    _name = ‘child.model’

    

    _inherits = {‘parent.model’: ‘parent_id’}

    parent_id = fields.Many2one(‘parent.model’)

    

    email = fields.Char()

    

    @api.multi

    def do_something(self):

        # overriding parent method

        pass

In this example, the ChildModel inherits from the ParentModel using the _inherits attribute to create a copy of the ParentModel. The child model also adds a new field email and overrides the do_something method of the parent model.


For any inquiries, suggestions, or feedback, you can reach out to us through the provided contact form on our blog. We make an effort to respond to all queries in a timely manner and appreciate your engagement with our content.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *