Enable Dark Mode!
what-is-mixin-class-and-how-to-use-mixin-classes-in-odoo-18.jpg
By: Farha VC

What is Mixin Class & How to Use Mixin Classes in Odoo 18?

Technical

In the world of coding, one of the main significance of the codes we type is their reusability.  The reusability approach reduces the redundancy, saves time on repetitive tasks, and shrinks codebase size and thereby the maintainability gets improved. While talking about this in Odoo, the Mixin Class is the one feature that serves for the reusability of the code block that provides the additional functionality of sharing it across the different models used. 
This does not need the base class to get inherited directly in the needed module; rather we need to just create a class for the Mixin first and inherit it to any other class we need. 
Odoo 18  uses several mixin classes in the addon to provide features like tracking changes made, adding messaging and other communication capabilities (mail.thread), managing activities (mail.activity.mixin), managing portal access (portal.mixin), and so on. 
In addition to these built-in mixins, we can create and use our own custom mixins in Odoo using the AbstractModel class, which will reside in the database as a blueprint to be used by any models in the future. So, let’s have a look at how the mixins are used and managed in Odoo 18.

Creating a custom mixin

In Odoo 18, for demonstrating the creation and usage of a Mixin class, let’s take an example of creating a Mixin class for calculating the age of a person with the provided date of birth and current date.
For that, first, we need to create the structure of the Mixin class using the AbstractModel, here for example, the CalculateAge, and define the fields and the required methods for the age calculation.
from odoo import fields, models, api
from datetime import date
class CalculateAge(models.AbstractModel):
   _name = 'calculate.age'
   _description = 'Mixin for calculating the age'
   date_of_birth = fields.Date("Date of Birth", required=True)
   age = fields.Float(string="Age", compute='_compute_age')
   @api.depends("date_of_birth")
   def _compute_age(self):
       today = date.today()
       current_year = today.year
       for record in self:
           if record.date_of_birth:
               record.age = current_year-record.date_of_birth.year
In this Mixin, the fields ‘date_of_birth’ and ‘age’ are defined and there is a computer method ‘_compute_age()’ that calculates the age of the person based on the date of birth given. 
Now, it’s time to use the Mixin class ‘CalculateAge’ in some other models, probably in the custom ones.
So, let’s create a model ‘student.student’ in a new module for holding College records. Here, we just need to inherit the abstract mixin class to the custom model to calculate the age of the student records that are going to be created. The Python code for defining the class is as
from odoo import fields, models
class Student(models.Model):
   _name = 'student.student'
   _inherit = 'calculate.age'
   name = fields.Char("Student Name")
   class_name = fields.Char("Class")
   roll_no = fields.Char("Roll Number")
Now, inherit the same mixin class for another named ‘teacher.teacher’ as an example here. Extend the features of the mixin class ‘CalculateAge’ to calculate the age of the teacher records that are going to be created.
from odoo import fields, models
class Teacher(models.Model):
   _name = 'teacher.teacher'
   _inherit = 'calculate.age'
   name = fields.Char("Teacher Name")
   subject = fields.Char("Subject Name")
   dept = fields.Char("Department")
Both of these custom models will now have their own fields and methods defined along with the ones that are defined in the CalculateAge Mixin.
 To see the use of this concept, let’s create the form views for the models ‘Student’ and ‘Teacher’ and add their fields along with the ‘date_of_birth’ and ‘age’ fields.
By encompassing both the form views in a single XML file for the easiness of the demonstration, we add code like
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!--    Form view for the Teacher class-->
   <record id="teacher_teacher_view_form" model="ir.ui.view">
       <field name="name">teacher.teacher.view.form</field>
       <field name="model">teacher.teacher</field>
       <field name="arch" type="xml">
           <form string="Teacher List">
               <sheet>
                   <group>
                       <group>
     <!--   Adding fields of Teacher class-->
                       <field name="name"/>
                       <field name="subject"/>
                       <field name="dept"/>
                   </group>
          <!--   Including fields of CalculateAge Mixin-->
                   <group>
                       <field name="date_of_birth"/>
                       <field name="age"/>
                   </group>
                   </group>
               </sheet>
           </form>
       </field>
   </record>
<!--    Form view for the Teacher class-->
<record id="student_student_view_form" model="ir.ui.view">
   <field name="name">student.student.view.form</field>
   <field name="model">student.student</field>
   <field name="arch" type="xml">
       <form string="Student List">
           <sheet>
               <group>
                   <group>
          <!--   Adding fields of Student class-->
                       <field name="name"/>
                       <field name="class_name"/>
                       <field name="roll_no"/>
                   </group>
                   <group>
          <!--   Including fields of CalculateAge Mixin-->
                       <field name="date_of_birth"/>
                       <field name="age"/>
                   </group>
               </group>

           </sheet>
       </form>
   </field>
</record>
By these, we can see that on the form view of both the custom models Student’ and ‘Teacher’, the respective fields from the model and the Mixin class will be shown on the UI, and the mixin method _compute_age() also works for both as we enter the value to the ‘date_of_birth’ field. That is, whatever we define in a mixin class, gets shared with all the other models in the database when the mixin class is inherited this way. This demonstrates how a mixin class can be used to encapsulate and reuse common functionality for computing the age across different classes.
In conclusion, the Mixins in Odoo 18 are designed for defining common fields and methods in a class that can be shared among multiple models. These Mixins are just residing in the database with the defined property. They cannot be instantiated on their own, which means they get a life only when any other model makes use of them through inheritance. Multiple combinations of mixins can be used in a module so that it can possess all the additional features from all the inherited mixins. Mixins make the Odoo codes modular and maintainable in every sense. If you want to know more about How to Use Mixin Classes in Odoo, refer to our previous blog.


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