Odoo 19 still has the ability to provide powerful and developer-friendly ways to extend business logic without modifying the code. Among the most useful ways to write clean, reusable, and maintainable code is by using mixin classes.
Mixin classes give you the ability to share common logic among multiple models without having to write the same code multiple times. Instead of duplicating code in models, you can inject additional logic wherever it is needed.
In this blog, we’ll explore:
- What mixin classes are in Odoo
- Why they matter in real-world projects
- How to create and use mixins in Odoo 19
- Best practices and common pitfalls
- Frequently asked questions about mixins
What Is a Mixin Class in Odoo?
Mixin class in Odoo is an abstract model that has reusable fields or methods. It is not intended to store data or function on its own. Rather, it serves as a blueprint that can be inherited by other models to extend their functionality.
In simple terms:
- A mixin adds functionality
- It does not replace or override core models
- It encourages modular, reusable, and scalable code
Common use cases for mixins include:
- Activity logging
- Validation rules
- helper methods
- Shared computed fields
- Access control or audit features
Why Use Mixins in Odoo 19?
Using mixin classes brings several advantages that align perfectly with Odoo’s modular architecture:
- Avoid code duplication across models
- Keep business logic separate from core model definitions
- Improve maintainability and readability
- Extend multiple models consistently
- Follow Odoo best practices for clean architecture
In large Odoo implementations, mixins become essential for keeping modules manageable and future-proof.
Step 1: Create a Mixin Class
Let’s create a simple mixin that logs activities when certain actions occur.
In Odoo 19, mixins are defined using models.AbstractModel.
from odoo import models, api
import logging
_logger = logging.getLogger(__name__)
class ActivityLoggerMixin(models.AbstractModel):
_name = 'activity.logger.mixin'
_description = 'Activity Logger Mixin'
@api.model
def log_activity(self, message):
_logger.info(message)
Key Points:
- models.AbstractModel ensures no database table is created
- The mixin provides shared behavior only
- The method can be reused by any model that inherits it
This mixin can now be plugged into multiple models without rewriting logging logic.
Step 2: Extend an Existing Model Using the Mixin
Now let’s apply this mixin to an existing model, such as res.partner.
from odoo import models, api
class ResPartner(models.Model):
_inherit = ['res.partner', 'activity.logger.mixin']
@api.model
def create(self, vals):
record = super().create(vals)
self.log_activity(f'Partner created: {record.name}')
return record
What’s Happening Here?
- The model inherits both res.partner and the mixin
- res.partner gains access to log_activity()
- Each time a partner is created, an activity log is generated
- No core code is modified
- The same mixin can be reused in other models like sale.order, account.move, etc.
This approach keeps your customization clean, upgrade-safe, and reusable.
Advantages of Using Mixin Classes
1. Reusability
Write logic once and reuse it across multiple models.
2. Clean Code Structure
Business logic stays isolated, making models easier to read and maintain.
3. Easy Extensibility
Enhance or update functionality in one place without touching every model.
4. Better Maintainability
Fixes or improvements in the mixin automatically apply everywhere it’s used.
Best Practices for Mixins in Odoo 19
To get the most out of mixins, follow these guidelines:
- Always define mixins using models.AbstractModel
- Never create records for mixin models
- Keep mixins focused on a single responsibility
- Use clear and unique method names to avoid conflicts
- Avoid putting model-specific business logic inside mixins
- Document mixins well if they are shared across modules
Common Mistakes to Avoid
- Using models.Model instead of AbstractModel
- Adding fields that imply record storage
- Overloading mixins with unrelated logic
- Overriding critical core methods without calling super()
Mixin classes are a powerful yet elegant concept in Odoo 19 that help developers build scalable, reusable, and maintainable modules. Whether you need logging, validation, or shared utilities, mixins allow you to extend models cleanly without touching core code.
By embracing mixins, you:
- Write smarter code
- Reduce duplication
- Improve maintainability
- Follow Odoo best practices
If your goal is professional, upgrade-safe Odoo development, mixin classes should be a core part of your toolkit.
To read more about How to Extend Models Using Mixin Classes in Odoo 18, refer to our blog How to Extend Models Using Mixin Classes in Odoo 18.