Enable Dark Mode!
what-is-mixin-class-and-how-to-use-mixin-classes-in-odoo-16.jpg
By: Saleekha Musthari

What is Mixin Class & How to Use Mixin Classes in Odoo 16

Technical Odoo 16

A mixin is a class in Python that offers reusable method implementations. A group of methods is simply bundled for reuse with mixins. In the context of Odoo, a popular open-source ERP and business application platform, the use of mixin classes offers a powerful mechanism for creating modular and reusable code. Mixins in Odoo can encapsulate common functionalities or behaviors, enabling developers to efficiently extend and enhance the capabilities of their models.

The versatility of mixin classes in Odoo becomes particularly evident when aiming to extend functionalities across multiple models. Developers can efficiently leverage mixins to avoid code duplication, promote a cleaner codebase, and simplify future modifications or enhancements.

Incorporating mixin classes into Odoo development practices not only streamlines the coding process but also contributes to the creation of more adaptable and scalable applications. As a best practice, understanding when and how to employ mixin classes ensures a more efficient and robust development workflow in the Odoo 16 framework.

This feature is supported in the Odoo platform, and this blog covers how to use mixin classes in Odoo 16.

Certainly! Let's create an example where we use a mixin class to compute grades for different classes. The mixin class will include a method for computing the grade based on the marks obtained by students. We'll then use this mixin in models representing different classes.

from odoo import models, fields, api
class GradeComputableMixin(models.AbstractModel):
   _name = 'grade.computable.mixin'
   _description = 'Grade Computable Mixin'
   marks_obtained = fields.Float(string='Marks Obtained', required=True)
   grade = fields.Char(string='Grade', compute='_compute_grade', store=True)
   @api.depends('marks_obtained')
   def _compute_grade(self):
       for record in self:
           marks = record.marks_obtained
           if 90 <= marks <= 100:
               record.grade = 'A+'
           elif 80 <= marks < 90:
               record.grade = 'A'
           elif 70 <= marks < 80:
               record.grade = 'B'
           elif 60 <= marks < 70:
               record.grade = 'C'
           else:
               record.grade = 'D'

You can use the same mixin in multiple models by simply inheriting from it.

In this example, we'll inherit the mixin class in the ClassAStudent model to demonstrate how it can be used to compute grades for Class A Students.

class ClassAStudent(models.Model):
   _name = 'class_a.student'
   _description = 'Class A Student'
   _inherit = ['grade.computable.mixin']
   name = fields.Char(string='Name', required=True)
   roll_number = fields.Char(string='Roll Number')

In this example, the ClassBStudent model inherits from both models. Model and GradeComputableMixin. This enables the model to include fields specific to Class B students while utilizing the mixin's capability to calculate grades based on marks obtained.

class ClassBStudent(models.Model):
   _name = 'class_b.student'
   _description = 'Class B Student'
   _inherit = ['grade.computable.mixin']
   name = fields.Char(string='Name', required=True)
   roll_number = fields.Char(string='Roll Number')

In the below example, the ClassCStudent model inherits from both models. Model and the GradeComputableMixin. This design allows the model to incorporate specific fields for Class C students while benefiting from the mixin's ability to compute grades based on the marks obtained.

class ClassCStudent(models.Model):
   _name = 'class_c.student'
   _description = 'Class C Student'
   _inherit = ['grade.computable.mixin']
   name = fields.Char(string='Name', required=True)
   roll_number = fields.Char(string='Roll Number')

Then you can include the fields defined in the GradeComputableMixin (abstract model) in the form views of ClassAStudent, ClassBStudent, and ClassCStudent, you can modify the XML code accordingly. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
   <!-- Form view for ClassAStudent -->
   <record model="ir.ui.view" id="class_a_student_form">
       <field name="name">class_a.student.form</field>
       <field name="model">class_a.student</field>
       <field name="arch" type="xml">
           <form>
               <sheet>
                   <group>
                       <!-- Include fields from GradeComputableMixin -->
                       <field name="marks_obtained"/>
                       <field name="grade"/>
                       <!-- Other fields specific to ClassAStudent -->
                       <field name="name"/>
                       <field name="roll_number"/>
                       <!-- Additional fields as needed -->
                   </group>
               </sheet>
           </form>
       </field>
   </record>
   <!-- Form view for ClassBStudent -->
   <record model="ir.ui.view" id="class_b_student_form">
       <field name="name">class_b.student.form</field>
       <field name="model">class_b.student</field>
       <field name="arch" type="xml">
           <form>
               <sheet>
                   <group>
                       <!-- Include fields from GradeComputableMixin -->
                       <field name="marks_obtained"/>
                       <field name="grade"/>
                       <!-- Other fields specific to ClassBStudent -->
                       <field name="name"/>
                       <field name="roll_number"/>
                       <!-- Additional fields as needed -->
                   </group>
               </sheet>
           </form>
       </field>
   </record>
   <!-- Form view for ClassCStudent -->
   <record model="ir.ui.view" id="class_c_student_form">
       <field name="name">class_c.student.form</field>
       <field name="model">class_c.student</field>
       <field name="arch" type="xml">
           <form>
               <sheet>
                   <group>
                       <!-- Include fields from GradeComputableMixin -->
                       <field name="marks_obtained"/>
                       <field name="grade"/>
                       <!-- Other fields specific to ClassCStudent -->
                       <field name="name"/>
                       <field name="roll_number"/>
                       <!-- Additional fields as needed -->
                   </group>
               </sheet>
           </form>
       </field>
   </record>
</odoo>

Brifley in this example the GradeComputableMixin mixin class includes fields for marks obtained and grade, along with a computed method '_compute_grade' that calculates the grade based on the marks obtained.

The ClassAStudent, ClassBStudent, and ClassCStudent models inherit from the GradeComputableMixin, allowing them to share the fields and the grade computation method.

Now, whenever you create a student record for Class A, Class B, or Class C, the grade will be automatically computed based on the marks obtained. This demonstrates how a mixin class can be used to encapsulate and reuse common functionality for computing grades across different classes.

In conclusion, mixin classes in Odoo provide a valuable tool for developers to enhance code organization, reusability, and maintainability. By encapsulating reusable methods, mixins enable the modular construction of code components that can be easily integrated into various classes.

To read more about extending models using mixin classes in Odoo 16, refer to our blog How to Extend Models Using Mixin Classes in Odoo 16


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
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