Enable Dark Mode!
how-extend-models-using-mixin-classes-in-odoo-19.jpg
By: Najiya Rafi

How Extend Models Using Mixin Classes in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

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.


Frequently Asked Questions

Can a mixin have fields in Odoo?

Yes, a mixin can define fields. These fields will appear in any model that inherits the mixin. However, ensure they are truly generic and reusable.

Can multiple mixins be inherited in one model?

Absolutely. A model can inherit multiple mixins: _inherit = ['res.partner', 'activity.logger.mixin', 'another.mixin'] Odoo will merge all behaviors into the final model.

Do mixins create database tables?

No. Since mixins use models.AbstractModel, they do not create database tables.

When should I use a mixin instead of _inherit?

Use a mixin when the logic needs to be reused across several unrelated models and the behavior is generic rather than tied to a specific business object. A mixin is ideal for adding shared features such as logging, approval flows, or tracking capabilities that multiple models can benefit from without being directly related to each other. On the other hand, use _inherit by itself when you are extending or modifying the behavior of a single existing model. This approach is appropriate when the changes are specific to that particular model and are not intended to be reused elsewhere.

Can mixins override core methods?

Yes, but it should be done carefully. Always call super() to avoid breaking existing logic.

Are mixins upgrade-safe in Odoo?

Yes. Since mixins do not modify core code directly, they are one of the safest customization approaches for upgrades.

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
Kakkanchery, 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