Introduction

As an Odoo Consultant, understanding the concept of Mixins is essential for creating scalable, reusable, and efficient modules in Odoo 19. Mixins are a powerful feature that allows developers to extend existing models with predefined functionalities without repeating code.

They bring modularity, flexibility, and maintainability to the Odoo framework, enabling developers to add shared behaviors across multiple models seamlessly. In this guide, we’ll explore what Mixins are, how they work, their built-in types in Odoo 19, and how you can create your own to enhance your development workflow.


What Are Mixins in Odoo 19?

A Mixin in Odoo is an abstract model that provides reusable functionality to other models. Unlike traditional class inheritance (where one model inherits from another), a Mixin can be combined with multiple models simultaneously, offering a modular approach to extending functionalities.

For instance, suppose several models in your Odoo module require email communication capabilities. Instead of writing the same logic repeatedly, you can inherit from the mail.thread mixin, which already includes messaging, notifications, and chatter support.

This concept helps in maintaining a cleaner code structure, improving reusability, and minimizing redundancy.


Why Use Mixins in Odoo 19

Using Mixins brings numerous benefits to Odoo developers and consultants. Let’s explore the main advantages:

  1. Code Reusability
    Mixins allow you to define reusable logic once and apply it across multiple models. This significantly reduces code duplication.

  2. Modularity
    Each Mixin encapsulates specific functionality (e.g., communication, website publishing, portal access), keeping code organized and easier to maintain.

  3. Flexibility
    You can easily enable or disable certain functionalities by adding or removing mixin inheritance from a model.

  4. Maintainability
    Centralized logic in mixins makes it easier to apply updates or bug fixes across all models that use them.

  5. Clean Inheritance Chain
    Mixins prevent the complications of deep inheritance hierarchies by providing self-contained, independent features.


Common Built-in Mixins in Odoo 19

Odoo 19 comes with a variety of built-in mixins that handle common functionalities across modules. These mixins simplify development by offering pre-implemented logic.

Let’s look at some of the most commonly used ones.


1. MailThread Mixin

Location: odoo.addons.mail.models.mail_thread

The MailThread mixin enables communication features within your model. It adds Odoo’s Chatter functionality — allowing users to send messages, log notes, schedule activities, and follow records.

Key Features:

  • Enables the chatter widget on form views.

  • Supports emails and internal messaging.

  • Tracks activities, attachments, and communication history.

  • Allows followers to receive record updates.

Example:

from odoo import fields, models class MyModel(models.Model): _name = 'my.model' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = 'Model with Chatter Support' name = fields.Char(string='Name', required=True) description = fields.Text(string='Description')

By inheriting mail.thread, your model automatically supports chatter features with no extra configuration.


2. PortalMixin

Location: odoo.addons.portal.models.portal_mixin

The PortalMixin allows external users (like customers) to access records securely through the portal. It manages record sharing, access tokens, and personalized URLs.

Key Features:

  • Provides access to external portal users.

  • Generates secure record URLs using tokens.

  • Customizable access paths and redirection.

Example:

from odoo import fields, models class MyPortalModel(models.Model): _name = 'my.portal.model' _inherit = ['portal.mixin'] _description = 'Model with Portal Access' name = fields.Char(string='Name', required=True) partner_id = fields.Many2one('res.partner', string='Customer') def _compute_access_url(self): super()._compute_access_url() for record in self: record.access_url = '/my/portal/%s' % record.id

After adding this mixin, users can share secure record links with customers or partners directly.


3. WebsiteMixin

Location: odoo.addons.website.models.website

The WebsiteMixin enables your model to be published on the Odoo website. It adds fields and logic for SEO optimization, publication control, and URL generation.

Key Features:

  • Adds a website_published boolean to control visibility.

  • Includes SEO metadata fields.

  • Manages website-friendly URLs.

Example:

from odoo import fields, models class MyWebsiteModel(models.Model): _name = 'my.website.model' _inherit = ['website.mixin'] _description = 'Model for Website Integration' name = fields.Char(string='Title', required=True) content = fields.Html(string='Content') website_url = fields.Char(string='Website URL', compute='_compute_website_url') def _compute_website_url(self): for record in self: record.website_url = "/my-content/%s" % record.id

Once integrated, your content can be directly published and managed from Odoo’s website module.


4. RatingMixin

Location: odoo.addons.rating.models.rating_mixin

The RatingMixin is commonly used in Helpdesk, eCommerce, or Service applications to gather customer feedback.

Example:

from odoo import fields, models class MyRatingModel(models.Model): _name = 'my.rating.model' _inherit = ['rating.mixin'] _description = 'Model with Rating Feature' name = fields.Char(string='Service Name', required=True)

This mixin automatically adds rating-related fields like rating_avg and rating_last_value, enabling you to track service or product quality.


5. Sequence Pattern (Mixin-like Behavior)

Although not a true mixin, Odoo’s sequence pattern behaves similarly by promoting code reuse. It uses ir.sequence to generate unique identifiers for records.

Example:

from odoo import api, fields, models class MyModel(models.Model): _name = 'my.model' _description = 'Model with Sequence' name = fields.Char( string='Reference', readonly=True, copy=False, default=lambda self: self.env['ir.sequence'].next_by_code('my.model.sequence') )

XML for Sequence:

 
<odoo> <data noupdate="1"> <record id="seq_my_model" model="ir.sequence"> <field name="name">My Model Sequence</field> <field name="code">my.model.sequence</field> <field name="prefix">MYM</field> <field name="padding">4</field> </record> </data> </odoo>

With this, every new record automatically gets a unique code like MYM0001.


Creating a Custom Mixin

Beyond the built-in mixins, Odoo 19 lets developers create their own. Let’s build a simple AuditableMixin to track who created and last modified a record.

Mixin Definition:

from odoo import fields, models, api class AuditableMixin(models.AbstractModel): _name = 'auditable.mixin' _description = 'Tracks Created and Modified Users' created_by_id = fields.Many2one('res.users', string='Created By', readonly=True, default=lambda self: self.env.user) last_modified_by_id = fields.Many2one('res.users', string='Last Modified By', readonly=True) @api.model_create_multi def create(self, vals_list): records = super().create(vals_list) for record in records: record.last_modified_by_id = self.env.user return records def write(self, vals): vals['last_modified_by_id'] = self.env.user.id return super().write(vals)

Use in a Model:

from odoo import fields, models class MyModel(models.Model): _name = 'my.model' _inherit = ['auditable.mixin'] _description = 'Model Using Custom Auditable Mixin' name = fields.Char(string='Name', required=True)

Explanation:
This mixin automatically tracks who created and last modified each record. You can reuse this across multiple models without rewriting logic — a perfect demonstration of modular design.


Best Practices for Using Mixins in Odoo

  1. Keep Mixins Lightweight:
    Include only essential logic and avoid dependencies between mixins.

  2. Use Meaningful Names:
    Name your mixins according to their function (e.g., TrackableMixin, ApprovalMixin).

  3. Combine Mixins Wisely:
    Avoid adding too many mixins to a single model to maintain clarity.

  4. Document Your Mixin Logic:
    Always include docstrings and comments for other developers’ reference.

  5. Use Abstract Models (models.AbstractModel):
    Define custom mixins as abstract to prevent them from creating database tables.


Conclusion

Mixins in Odoo 19 are one of the most powerful tools for achieving modularity and code reusability. For any Odoo Consultant or developer, mastering Mixins means writing cleaner, more efficient, and maintainable code. Whether it’s adding communication features, portal access, website publication, or creating your own reusable logic, Mixins provide an elegant and scalable solution to extend Odoo models.

By understanding and applying Mixins effectively, you can streamline development, improve performance, and deliver professional-grade Odoo implementations tailored to business needs.

Book an implementation consultant today.