Enable Dark Mode!
different-types-of-inheritance-in-odoo-16.jpg
By: Swapna

Different Types of Inheritance in Odoo 16

Technical Odoo 16

Here we are going to discuss the different types of inheritance in Odoo 16, and there are three types of inheritance.

Classic Inheritance:

This is the most basic and commonly used inheritance method in Odoo. It involves creating a new class that inherits from an existing class and modifying or adding functionality to it. The original class is called the parent or base class, while the new class is called the child or derived class.
Here's an example of how classic inheritance works in Odoo:
Suppose we have a base class called "ProductProduct" that has the following fields: name(string), description (text), price (float), quantity (integer)
Now, we want to create a new class called "SaleProduct" that inherits from the "ProductProduct" class but with some modifications. Specifically, we want to add a new field called "discount" and override the price calculation method to take the discount into account.
Here's how we can implement this in Odoo using classic inheritance:
class ProductProduct(models.Model):    _name = 'product.product'        name = fields.Char(required=True)    description = fields.Text()    price = fields.Float(digits=(6, 2))    quantity = fields.Integer(default=0)class SaleProduct(models.Model):    _name = 'sale.product'    _inherit = 'product.product'        discount = fields.Float(digits=(4, 2))        @api.depends('price', 'discount')    def _compute_sale_price(self):        for product in self:            product.sale_price = product.price * (1 - (product.discount / 100))                sale_price = fields.Float(digits=(6, 2), compute='_compute_sale_price', store=True)
In this example, we define a new class called "SaleProduct" that inherits from the "ProductProduct" class using the _inherit attribute. We add a new field called "discount" to the "SaleProduct" class, and we override the _compute_sale_price method to calculate the sale price by taking the discount into account.
Now, when we create a new "SaleProduct" object, it will have all the fields and methods of the "ProductProduct" class, plus the new "discount" field and the overridden price calculation method.

Prototype Inheritance:

Prototype inheritance is a powerful feature in Odoo that allows you to create new objects based on existing objects. This type of inheritance is used to create new objects with specific functionality or attributes while keeping the original object unchanged.
Here's an example of how prototype inheritance works in Odoo:
Suppose we have an existing model called "res.partner" that represents a partner or customer in our system. The "res.partner" model has several fields, such as "name", "email", and "phone".
Now, we want to create a new model called "res.customer" that inherits from the "res.partner" model but with some modifications. Specifically, we want to add a new field called "loyalty_points" and override the "create" method to assign loyalty points to the customer.
Here's how we can implement this in Odoo using prototype inheritance:
class ResPartner(models.Model):    _inherit = 'res.partner'        loyalty_points = fields.Integer(default=0)class ResCustomer(models.Model):    _inherit = 'res.partner'    _name = 'res.customer'        @api.model    def create(self, vals):        vals['loyalty_points'] = 100  # Assign 100 loyalty points to new customers        return super(ResCustomer, self).create(vals)
In this example, we define a new class called "ResCustomer" that inherits from the "res.partner" class using the _inherit attribute. We also set the _name attribute to 'res.customer' to give the new model a distinct name.
We then override the "create" method of the "ResCustomer" class to assign 100 loyalty points to new customers by default. When we create a new "ResCustomer" object, it will have all the fields and methods of the "Res.partner" class, plus the new "loyalty_points" field and the overridden "create" method.
Note that we don't need to define the "loyalty_points" field in the "ResCustomer" class because it is already defined in the parent "res.partner" class. We are simply inheriting the existing field and adding it to the "ResCustomer" class. This is the essence of prototype inheritance - creating new objects based on existing objects with specific modifications or additions.

Delegation Inheritance:

Delegation inheritance is similar to prototype inheritance but with a different approach. In delegation inheritance, you create a new object that delegates some of its functionality to an existing object. This type of inheritance is useful when you want to add new functionality to an object without modifying its original behavior.
Here's an example of how delegation inheritance works in Odoo:
Suppose we have an existing model called "res.partner" that represents a partner or customer in our system. The "res.partner" model has several fields, such as "name", "email", and "phone".
Now, we want to create a new model called "res.customer" that delegates some of its functionality to the "res.partner" model. Specifically, we want to override the "name_get" method of the "res.customer" model to add a prefix "Customer: " to the name of the customer.
Here's how we can implement this in Odoo using delegation inheritance:
class ResPartner(models.Model):    _inherit = 'res.partner'class ResCustomer(models.Model):    _name = 'res.customer'        partner_id = fields.Many2one('res.partner', required=True, ondelete='cascade')        def name_get(self):        result = []        for customer in self:            name = customer.partner_id.name_get()[0][1]            result.append((customer.id, "Customer: %s" % name))        return result
In this example, we define a new class called "ResCustomer" that has a many-to-one relationship with the "res.partner" class using the "partner_id" field. This allows us to delegate some of the functionality of the "res.customer" class to the "res.partner" class.
We then override the "name_get" method of the "res.customer" class to add a prefix "Customer: " to the name of the customer. Inside the method, we call the "name_get" method of the "res.partner" class using the "partner_id" field to get the name of the partner, and then add the prefix "Customer: " to it.
Now, when we call the "name_get" method on a "res.customer" object, it will delegate the name retrieval to the associated "res.partner" object and add the prefix "Customer: " to it. This is the essence of delegation inheritance - creating a new object that delegates some of its functionality to an existing object.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message