Polymorphic model inheritance in Odoo

I should admit that I still haven’t implemented this type of inheritance in my custom modules, but I have used models that already had it, such as product.template and product.product.

If you try to analyze these models you will see that in the product.product model there is field product_tmpl_id, that is a Many2one relation with the product.template and every time when you create a new product.product object automatically a new product.template object is created for it. Additionally, you can access product.template fields from the product.product object.

In order to implement such inheritance in a model, you need to define the attribute _inherits as a dictionary with a key existing model’s name and a value name of the field that will be represented in the model. In the example bellow, you can see how it is implemented in the product.product model.

from openerp import models

class Product(models.Model):
    _name = "product.product"
    _inherits = {'product.template': 'product_tmpl_id'}

Also, you can find more info for this inheritance in the Q&A section of Odoo or follow the link.

One of the use case scenarios for this inheritance is when you need to store some additional data that need to be separated in a new table and you don’t want to duplicate the fields from the existing model.

Feel free to share your use case as a comment on this post.

Leave a Reply

Your email address will not be published.