Enable Dark Mode!
how-to-create-mixins-in-odoo-18.jpg
By: Shafna K

How to Create Mixins in Odoo 18

Technical

Odoo 18 introduces powerful tools for developers to create reusable and modular code. One such tool is the use of Mixins. Mixins are lightweight classes that provide shared functionality to multiple models without the need for deep inheritance hierarchies. This blog will guide you through the concept, benefits, and steps to create and use Mixins in Odoo 18 effectively.

What Are Mixins in Odoo?

Mixins in Odoo are base classes that are inherited by other models to add specific functionality. They allow developers to define common behavior in one place and reuse it across multiple models, promoting code reusability and reducing redundancy.

For example, you might have a Mixin class that adds logging, timestamping, or other shared behaviors to multiple models in your application.

Benefits of Using Mixins

* Code Reusability: Write once, reuse everywhere.

* Modular Design: Keeps your codebase clean and maintainable.

* Flexibility: Add functionality to models without altering their primary inheritance chain.

* Consistency: Ensures the same behavior is applied across multiple models.

Steps to Create Mixins in Odoo 18

Step 1: Define the Mixin Class

To create a Mixin, define a new class that inherits from models.AbstractModel. Abstract models are not directly tied to database tables but can be inherited by other models.

Example: Logging Mixin

The following example demonstrates how to create a Logging Mixin that records updates to any inherited model:

from odoo import models
class LoggingMixin(models.AbstractModel):
    _name = 'mixin.logging'
    _description = 'Logging Mixin'
    def write(self, vals):
        for record in self:
            self.env['mail.message'].create({
                'body': f'Record {record.id} updated with {vals}',
                'model': record._name,
                'res_id': record.id
            })
        return super().write(vals)

The function overrides the write method to create a log message whenever a record is updated.

Step 2: Use the Mixin in a Model

Once the Mixin is defined, it can be inherited by any model that requires the functionality.

from odoo import models
class SaleOrder(models.Model):
    _inherit = 'sale.order'
    _name = 'sale.order'
    _inherit = ['sale.order', 'mixin.logging']

_inherit specifies the models being inherited. The sale.order model now inherits the functionality of the mixin.logging Mixin.

Mixins are a powerful feature in Odoo 18 that enables developers to create modular and reusable code. By encapsulating common functionality into Mixin classes, you can enhance consistency and reduce redundancy across your application. Whether it’s logging, timestamping, or custom behaviors, Mixins provide a clean and efficient way to extend your models.

To read more about how to Create Mixins in Odoo 17, refer to our blog How to Create Mixins in Odoo 17.


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