Odoo 18 introduces new enhancements and refinements across its ORM and framework, and one of the powerful features that developers rely on is the use of mixins and reusable utility classes. Mixins provide shared behavior that can be inherited across multiple models without repeating code, while utility classes offer commonly needed functionality in a structured way. In this blog, we'll explore key mixins and classes in Odoo 18 that can streamline your development.
What are Mixins in Odoo?
Mixins in Odoo are abstract models that are not meant to be used on their own but are designed to be inherited by other models. They provide common logic, fields, and methods that can be reused across multiple models.
from odoo import models
class MyCustomMixin(models.AbstractModel):
_name = 'my.custom.mixin'
_description = 'My Custom Mixin'
def common_logic(self):
# shared behavior
pass
Common Mixins in Odoo 18
1. mail.thread
This mixin allows models to use the messaging and notification features in Odoo. When added, records have a chatter section to track communication, log messages, and record field changes. It also supports automatic logging of tracked fields.
class MyModel(models.Model):
_inherit = ['mail.thread']
_name = 'my.model'
name = fields.Char(tracking=True)
2. mail.activity.mixin
Used for managing activities such as tasks, meetings, or calls. It enables the "Activities" section in form views, allowing users to schedule follow-ups and reminders.
class MyModel(models.Model):
_inherit = ['mail.activity.mixin']
3. portal.mixin
Grants portal access to records. When inherited, the record can be linked to a user’s portal dashboard, and methods like get_access_action() become available.
class MyModel(models.Model):
_inherit = 'portal.mixin'
4. rating.mixin
Allows users to submit feedback or ratings, commonly used in Helpdesk, Website, and eLearning modules. Includes computed fields like rating_avg, rating_count, and integration with rating templates.
class MyModel(models.Model):
_inherit = 'rating.mixin'
5. utm.mixin
Used to track UTM parameters for leads and campaigns. Useful in marketing analysis to trace where traffic and conversions are coming from.
class MyModel(models.Model):
_inherit = 'utm.mixin'
Custom Mixin Example:
from odoo import models, fields
class AuditMixin(models.AbstractModel):
_name = 'audit.mixin'
_description = 'Track creation and updates'
created_by = fields.Many2one('res.users', string='Created By', readonly=True)
updated_by = fields.Many2one('res.users', string='Updated By', readonly=True)
def write(self, vals):
vals['updated_by'] = self.env.user.id
return super().write(vals)
@api.model
def create(self, vals):
vals['created_by'] = self.env.user.id
return super().create(vals)
You can now inherit audit.mixin in any model to automatically track the user who created or updated records.
Mixins and base classes in Odoo 18 are powerful tools that promote clean, modular, and reusable code. Whether you're adding messaging to mail.thread, enabling portal access, or building custom logic with your own abstract models, understanding and leveraging these tools can significantly boost your productivity and code maintainability. Keep exploring the source code and documentation to find more hidden gems that can simplify your Odoo development.
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?