Chapter 4 - Odoo 15 Development Book

Extending Methods

Extending Methods:

Sometimes, there may be some requirements to perform some additional operations in an existing python method. This can be referred to as extending methods. When we are adding more features or customizing existing model classes this will be necessary to modify any existing business logic. Thereby there will be some cases where we need to modify the existing methods to adapt the added features.

Let us see this use case by extending a custom function. For that, consider a model that stores all the student record of an education organization.


                          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 in the form view of student record, which will automatically update the status field value of that student.


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

Define update_status() method in the python file. Here write method is used to write value to the student record set.


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

Consider that there is any requirement to inherit the existing student model to add some functionalities. For that, we can inherit student model and add an extra date field date_status_last_updated which will record the date on which status was updated lastly.


                         class StudentInherit(models.Model):
   _inherit = "student.student"
   date_status_last_updated = fields.Date(string="Status Updated Date")
                          

The next step is to extend the update status() method to update the field value of date_status_last_updated. For that define a method in the same name.This will help in extending an existing method.Then use the super() method to perform all the steps defined in the parent class. Super is used for supering a method which is already defined in the same class. Supering create() or write() methods are common in odoo. It is important point to remember that it is necessary to specify all arguments to the extending function as well. If there are any extra parameters in the parent function you must specify it in the inherited class as well. Below is the extended method that will first perform all the steps performed in the parent method and then perform the added steps in the extended method


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

By overriding existing method we can perform additional steps to an existing function without losing the actual functionality.

whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

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