Enable Dark Mode!
what-is-mixin-class-and-how-to-use-mixin-classes-in-odoo-19.jpg
By: Ayana R

What is Mixin Class & How to Use Mixin Classes in Odoo 19

Technical Odoo 19

When you start building modules in Odoo 19, one thing becomes obvious very quickly: many models need the same kind of features. Maybe several models need age calculation, or activity tracking, or messaging. Rewriting the same logic over and over again isn’t just tiring—it becomes a maintenance nightmare.

That’s exactly where mixin classes come in.

A mixin is basically a small, reusable bundle of functionality that you can “mix into” any model. Odoo has relied on mixins for years, and Odoo 19 continues the trend with smoother multiple inheritance and better ORM performance.

What is a Mixin Class?

A mixin class is an abstract model—meaning it doesn’t create its own database table and isn’t meant to be instantiated on its own. Instead, other models inherit it to gain its fields and methods.

What makes mixins useful?

  • Reusable by design: Declared using models.AbstractModel, so they’re strictly for sharing behavior.
  • Support for multiple inheritance: Odoo 19 handles method resolution order (MRO) more cleanly, even in complex inheritance chains.
  • No standalone records: You can't create a record of a mixin — it's just an add-on.
  • Focused scope: A good mixin does one thing well (e.g., notifications, calculations, portal access).

This pattern follows the DRY principle and makes your code far more modular.

Why Mixins Matter in Odoo 19

Odoo 19 introduces several ORM improvements, and mixins benefit directly from them.

Here’s why developers rely on mixins:

  • Cut down redundancy: Add a feature once, reuse it across many models.
  • Easy maintenance: Fix or improve the logic in one file, and every model using the mixin gets the update.
  • Tight integration with Odoo core: Built-in mixins like mail.thread or portal.mixin plug directly into Odoo apps.
  • Customizable for real-world needs: You can build mixins for very specific business rules, analytics, or compliance checks.

Compared to Odoo 18, version 19 performs better with computed fields in multi-inherited models and avoids many recompute bottlenecks.

How to Use Mixin Classes in Odoo 19

Let’s walk through a practical example: creating a custom age calculator mixin, then inheriting it in other models.

Step 1: Create a Custom Mixin

models/age_calculator_mixin.py

from odoo import models, fields, api
class AgeCalculatorMixin(models.AbstractModel):
    _name = 'age.calculator.mixin'
    _description = 'Mixin for Age Calculation'
    date_of_birth = fields.Date('Date of Birth', required=True)
    age = fields.Integer('Age', compute='_compute_age', store=True)
    @api.depends('date_of_birth')
    def _compute_age(self):
        today = fields.Date.today()
        for record in self:
            if record.date_of_birth:
                delta = today - record.date_of_birth
                record.age = delta.days // 365
            else:
                record.age = 0

This mixin introduces two fields — date_of_birth and a computed age.

Step 2: Inherit the Mixin in Any Model

Example: Employee model.

from odoo import models, fields
class Employee(models.Model):
    _name = 'my.model'
    _inherit = ['age.calculator.mixin']
    _description = 'Employee'
    name = fields.Char('Name', required=True)
    department = fields.Char('Department')

Example: Student model.

from odoo import models, fields
class SchoolStudent(models.Model):
    _name = 'school.student'
    _inherit = ['age.calculator.mixin']
    _description = 'Student'
    name = fields.Char('Name', required=True)
    grade = fields.Char('Grade')
Once inherited, the fields automatically appear in your views:
<group>
    <field name="name"/>
    <field name="department"/>
    <field name="date_of_birth"/>
    <field name="age" readonly="1"/>
</group>

Step 3: Override the Mixin’s Methods When Needed

Odoo 19 makes it safer to override mixin methods and ensures the dependency graph behaves correctly.

Example: Adding a tenure-based adjustment to an employee’s age.

from odoo import models, fields
class MyModel(models.Model):
    _name = 'my.model'
    _inherit = ['age.calculator.mixin']
    _description = 'Employee'
    hire_date = fields.Date('Hire Date')
    @api.depends('date_of_birth', 'hire_date')
    def _compute_age(self):
        super()._compute_age()  # Use existing logic first
        for record in self:
            if record.hire_date:
                tenure = fields.Date.today() - record.hire_date
                record.age += tenure.days // 365

The override still respects the mixin’s original computation, you’re simply extending it.

Mixin classes are one of the quiet superpowers of Odoo development. They let you build cleaner modules, avoid repeating yourself, and organize features in a way that scales as your application grows. Odoo 19 enhances this even further with better performance and smarter inheritance handling.

Whether you’re using built-in mixins like mail.thread or crafting your own, mixing functionality into models keeps your codebase both powerful and manageable.

To read more about What is Mixin Class & How to Use Mixin Classes in Odoo 18?, refer to our blog What is Mixin Class & How to Use Mixin Classes in Odoo 18?


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