Enable Dark Mode!
everything-you-need-to-know-about-mixins-in-odoo-19.jpg
By: Abhinraj R

Everything You Need to Know About Mixins in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

In Odoo 19, Mixins play a key role in creating clean, modular, and reusable code.They are not standalone models but act as feature injectors, allowing you to extend existing models with predefined logic, methods, and fields without rewriting the same functionality multiple times.

This article will walk you through the concept of mixins, their real-world use in Odoo, and how to build your own.

What Are Mixins in Odoo?

A Mixin in Odoo is a special kind of class that provides additional behavior to other models.

Unlike normal inheritance (where one class extends another), mixins can be combined with multiple models, letting developers share functionality in a modular way.

For example, if several models require email communication, instead of writing the logic repeatedly, you can simply inherit from the mail.thread mixin.

Why Use Mixins?

Using mixins in Odoo brings several advantages:

  1. Code Reusability:
  2. Avoid rewriting the same logic across multiple models.

  3. Modularity:
  4. Keep functionalities small, focused, and easy to maintain.

  5. Flexibility:
  6. Add or remove features from a model simply by including or excluding a mixin.

  7. Clean Inheritance Chain:
  8. Prevent complex multiple inheritance issues (like the diamond problem) by using self-contained, modular behaviors.

Common Built-in Mixins in Odoo 19

Odoo provides many built-in mixins that simplify common use cases.

Let’s explore a few that are widely used across the system.

MailThread Mixin

Location: odoo.addons.mail.models.mail_thread

The MailThread mixin is one of the most widely used in Odoo. It brings communication features like messages, activities, and followers to your model.

It automatically enables the Chatter panel on form views.

Key Features

  • Enables the chatter widget on records.
  • Allows users to follow records and receive notifications.
  • Supports email and internal messages.
  • Adds activity scheduling and attachments.
  • Keeps a history log of all communication.

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')

Once added, your model supports chatter, email notifications, and activity tracking — without extra configuration.

PortalMixin

Location: odoo.addons.portal.models.portal_mixin

The PortalMixin enables external users (such as customers) to view specific records in the customer portal. It adds access tokens, URLs, and sharing logic for portal interactions.

Key Features


  • Portal access for external users.
  • Secure record URLs using access tokens.
  • Customizable access paths.

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

The mixin automatically provides fields like access_token and access_url, which are essential for secure portal record sharing.

WebsiteMixin

Location: odoo.addons.website.models.website

The WebsiteMixin allows models to appear on the Odoo website. It is commonly used in websites and eCommerce modules to publish content online. It adds SEO and publication fields like website_published, website_meta_title, and URL helpers.

Key Features

  • Adds a website_published field for controlling visibility.
  • Includes SEO fields such as meta title and description.
  • 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

After inheriting from this mixin, your records can be published directly to your Odoo website.

RatingMixin

Location: odoo.addons.rating.models.rating_mixin

The RatingMixin introduces rating functionality commonly used in helpdesk tickets, services, and product 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)

After inheriting this mixin, the model automatically gains rating functionality that can be displayed in forms or website pages. This automatically provides fields like rating_ids, rating_last_value, and rating_avg.

Sequence Pattern (Mixin-like Behavior)

Although not a formal mixin, automatic sequence generation follows the same principle of 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')
   )
   @api.model_create_multi
   def create(self, vals_list):
       for vals in vals_list:
           if vals.get('name', '/') == '/':
               vals['name'] = self.env['ir.sequence'].next_by_code('my.model.sequence')
       return super().create(vals_list)

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>

Explanation:

Every time a record is created, Odoo automatically assigns the next sequence number defined in XML.

Creating a Custom Mixin

Beyond the built-in ones, you can define your own custom mixins to share specific logic across your modules.

Let’s build a simple AuditableMixin that tracks who created and last modified a record.

Define the Mixin

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 It 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:

Now, every record automatically tracks the user who created and last modified it. You can reuse this mixin in any model without rewriting the same logic.

Conclusion

Mixins in Odoo 19 offer a flexible and efficient way to build modular, reusable code. By grouping shared functionality into Mixin classes, developers can maintain cleaner code, ensure consistency, and minimize repetition throughout their applications. Whether used for logging, tracking timestamps, or implementing custom logic, Mixins provide a streamlined approach to extending and enhancing Odoo models.

To read more about How to Create Mixins in Odoo 18, refer to our blog How to Create Mixins 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