Odoo 17 Development Book-Extending Methods

Extending Methods

It may be necessary in some cases to add some additional operations to an already existing Python method. This is referred to as extending methods. We will need to modify any existing business logic when we add new features or customize existing model classes. As a result, we will need to modify existing methods in some cases to accommodate the new features.

Consider this use case by extending a custom function. Consider a model that stores all of the student records of a school.

from odoo import fields, models, api


class Student(models.Model):
    _name = "student.student"
    _description = "Student"

    name = fields.Char(string="Name", required=True)
    partner_id = fields.Many2one('res.partner', string="Partner")
    phone = fields.Char(string="Phone Number")
    email = fields.Char(string="Email", required=True)
    status = fields.Char(string="Status")

Add a button to a student record's form view that updates the student's status field value.

<header>
    <button name="update_status" string="Update Status" class="oe_highlight" type="object"/>
</header>

In the Python file, define the update_status() method. In this case, the write method is used to add a value to the student record set.

def update_status(self):
   self.write({
       'status': "Status Updated"
   })

Consider whether it is necessary to inherit the current student model in order to add certain functionalities. To accomplish this, we can inherit the student model and add a new date field date_status_last_updated, which will record when the status was last updated.

class StudentInherit(models.Model):
   _inherit = "student.student"

   date_status_last_updated = fields.Date(string="Status Updated Date")

By extending the update status() method, the date_status_last_updated field value must be updated. For this purpose, create a method with the same name. This will help to extend an existing method. The super() method can then be used to carry out all of the parent class's operations. Super is used to override an existing method in the same class. In Odoo, it is common to supersede the create() and write() methods. It is critical to keep in mind that all arguments to the extending function must also be specified. Any extra parameters from the parent function must be specified in the inherited class as well. The extended method is shown below, and it will first execute all of the parent method's steps before proceeding to the additional steps.

def update_status(self):
   res = super(Student, self).update_status()
   self.write({
       'date_status_last_updated': fields.Date.today()
   })
   return res

By overriding the existing method, we can add steps to an existing function without affecting its functionality.

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