In Python, a mixin is a class that provides reusable method implementations. Mixins are just a way to bundle together a bunch of methods for reuse. When used with Odoo, a well-known open-source ERP and business application platform, mixin classes provide an effective way to write reusable and modular code. Odoo mixins allow developers to effectively extend and improve the capabilities of their models by encapsulating common functionality or behaviors.
The adaptability of mixin classes in Odoo is especially noticeable when trying to expand features over several models. Mixins are an effective tool that developers may use to reduce code duplication, encourage a cleaner codebase, and make future changes and enhancements easier.
Mixin classes help to create more flexible and scalable applications by streamlining the coding process when they are integrated into Odoo development processes. Knowing when and how to use mixin classes is a best practice that guarantees a more reliable and effective development workflow in the Odoo 17 framework.
We will create a new abstract model for the Custom Mixin class.
# -*- coding: utf-8 -*-
import datetime
from odoo import fields, models
class CustomMixin(models.AbstractModel):
_name = "custom.mixin"
_description = "Custom Mixin"
Adding two fields
date_today = fields.Date(string="Date today", default=fields.Date.today())
date_time_now = fields.Datetime(string="Date today",
compute="_compute_date_time_now")
Adding the corresponding compute functions
def _compute_date_time_now(self):
for rec in self:
rec.date_time_now = fields.Datetime.now()
The compute function is used to get the current time.
Also adding a new method in the CustomMixin class. which is returning the date object for the string received.
def get_date(self, date_string):
return datetime.datetime.strptime(date_string, "%d%m%Y").date()
Now we will create a new model, "date.checker". We need to inherit the “custom.mixin” model for accessing the methods and fields of “custom.mixin” in our custom model. Also adding some fields in the model. Don’t forget to adding access for the users to the model.
# -*- coding: utf-8 -*-
from odoo import fields, models
class DateChecker(models.Model):
_name = "date.checker"
_inherit = ["custom.mixin"]
date_string = fields.Char("Date string")
time_now = fields.Char("Time Now")
date_converted = fields.Char("Date Converted")
Adding the corresponding views.
List view :
<record id="date_checker_view_tree" model="ir.ui.view">
<field name="name">date_checker_view_tree</field>
<field name="model">date.checker</field>
<field name="arch" type="xml">
<tree string="date_checker_tree">
<field name="date_converted"/>
<field name="date_today"/>
<field name="date_string"/>
</tree>
</field>
</record>
Form view :
<record id="date_checker_view_form" model="ir.ui.view">
<field name="name">date_checker_view_form</field>
<field name="model">date.checker</field>
<field name="arch" type="xml">
<form string="_form">
<header>
<button name="action_convert_date"
class="btn btn-primary"
string="Convert Date"
type="object"/>
<button name="action_current_time"
class="btn btn-primary"
string="Get Current Time"
type="object"/>
</header>
<sheet>
<group>
<field name="date_converted"/>
<field name="date_today"/>
<field name="time_now"/>
<field name="date_string"/>
</group>
</sheet>
</form>
</field>
</record>
In the view files, you can see the field “date_today” is included, which is not in the model “date.checker”. This field is part of the “custom.mixin” model. This is how we can access fields of a mixin model.
Now we can check how we can access the methods.
I have added two methods “action_current_time” and “action_convert_date” in the views.
def action_convert_date(self):
self.date_string = self.get_date(self.date_string)
The “action_convert_date” will help to convert the string to a date object using the “get_date” method in the “custom.mixin”.
def action_current_time(self):
self.time_now = str(self.date_time_now)
The “action_current_time” helps to get the current time from the “date_time_now” field.
“date_time_now” is a computed field that returns the current time.
In this example, the CustomMixin mixin class includes fields for marks date today and current time, along with the method “get_date” that is used to convert to a date object from the received string of dates and passes and a computed method “_compute_date_time_now” which is used to pass the current time when the function is performed.
The DateChecker models inherit from the CustomMixin, allowing them to share the fields and the “_compute_date_time_now” computation and “get_date” method.
Now, whenever you “action_current_time” performed “time_now” field will get a value automatically. This demonstrates how a mixin class can be used to encapsulate and reuse common functionality for using the same fields in different models. And we can use the “get_date” method to get the date object of a string.
To sum up, Odoo's mixin classes give developers a useful tool to improve the organization, reusability, and maintainability of their code. Mixins allow the modular building of code components that are readily merged into different classes by encapsulating reusable methods.