Guide to Creating and Using Mixins in Odoo 18
The essential principles to maintain clean scalable projects in Odoo development include code reusability together with modularity. Mixins in Odoo18 represents a vital concept which allows these principles to function effectively in Odoo 18 projects. Die following blog post explores Mixins starting from their definition and continues with custom Mixin creation instructions in Odoo 18 which leads to proper model implementation techniques. The article includes a step-by-step practical example that builds clarity around the concept.
What is a Mixins
Mixin in Odoo exists as a specific model type which functions to enhance various models with fields and methods along with behavioral features. A Mixin functions as a supporting class beyond physical model definition because it refuses to create database tables. A Mixin functions as a supporting module which other models can employ as inheritance sources. The implementation of Mixins occurs through abstract models that extend models.AbstractModel. Odoo integrates powerful Mixins into its core modules throughout the application. For example: Through the implementation of mail.thread a model can acquire message capabilities (messages and activities logging) with built-in chatter functionality. The portal.mixin Mixin enables portal functionalities accessible by customers. • rating.mixin: Adds rating and feedback capabilities. Your Mixins allow a more effective code reuse through standardized model behavior.
How to Create a Mixins in Odoo18
The process of creating a Mixin in Odoo remains simple. Here’s how you can do it:
Define the Mixin Class
To begin you need to develop a new Python class using models.AbstractModel as the inheritance base. The new class will require a _name and _description as well as common fields and methods which you want to reuse.
from odoo import models, fields
class MyCustomMixin(models.AbstractModel):
_name = 'my.custom.mixin'
_description = 'A Custom Mixin to Add Common Functionality'
common_field = fields.Char(string="Common Field")
def common_method(self):
for record in self:
record.common_field = "Updated by Mixin"
Explanation:
- _name: Gives the Mixin a technical name.
- common_field: A field that will be available in any model that inherits this Mixin.
- common_method: A method that updates the value of common_field.
Because it is an abstract model, no table will be created for my.custom.mixin in the database.
Inherit the Mixin in Another Model
Now that the Mixin is ready, you can inherit it in any model where you want to use its functionality.
Let’s add this feature to the sales order.
from odoo import models
class ResPartner(models.Model):
_inherit = ['res.partner', 'my.custom.mixin']
Important: When inheriting a Mixin and a real model together (like sale.order), you must define _inherit as a list.
Now, every Sale Order record will have:
- A new field: common_field.
- A method: common_method.
Using the Mixin
With this setup, using the Mixin’s features is easy. Use common_method freely in your application’s business logic, server-side actions, or button event handlers.
order = self.env['res.partner'].browse(1)
order.common_method()
print(order.common_field) # Output: "Updated by Mixin"
This demonstrates how the Mixin injects behavior and fields into any model seamlessly.
Why Should You Use Mixins
Mixins are incredibly useful for the following reasons:
- Code Reusability: Write once, use everywhere.
- Maintainability: Centralized changes — update logic in the Mixin, and all inherited models benefit immediately.
- Separation of Concerns: Keep common functionality separate from business-specific models.
- Scalability: As your module grows, you avoid duplication of common features.
Logging Changes with a Mixin
Imagine you want every model that uses your Mixin to automatically log a message whenever a record is created. Here’s how you could extend the Mixin:
from odoo import models, api
class AutoLoggerMixin(models.AbstractModel):
_name = 'auto.logger.mixin'
_description = 'Automatically Logs Creation of Records'
@api.model
def create(self, vals):
record = super(AutoLoggerMixin, self).create(vals)
record.message_post(body="A new record was created.")
return record
Then inherit it like this:
class PurchaseOrder(models.Model):
_inherit = [‘purchase.order’, ‘auto.logger.mixin’, ‘mail.thread’]
Result: Every time a Sale Order is created, a message will be automatically logged in the chatter!
Odoo18’s mixins offer an elegant and powerful method for sharing functionality between models.. By using Mixins, you can write cleaner, more modular, and scalable codebases. Whether you’re adding simple fields, complex methods, or even automating actions across models, Mixins are an essential tool for any serious Odoo developer.
Mixing in Odoo18 are key to building clean, reusable, and scalable applications also helps you to inject shared functionality into multiple models effortlessly, promoting code efficiency and easier maintenance.
Using the Mixins avoid the duplication and integrates common behaviors.
Want to optimize your Odoo development? Transines Solutions can help you leverage the power of Mixins to create efficient and maintainable Custom Odoo18 solutions to your needs. Contact us to Transines Solutions today to learn more.
"Automate Your Business with our Customized Odoo ERP Solutions"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote



