Odoo 16 Development Book

Extending Methods

In some cases, it might be necessary to add some additional operations to an already existing Python method. This is known as extending methods. When we add new features or customize existing model classes, we will need to modify any existing business logic. As a result, in some cases, we will need to modify the existing methods to adapt to the new features.

Let's take a look at this use case by extending a custom function. Consider a model that stores all of an educational organization's student records.

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 the form view of a student record that updates the student's status field value.

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

Define the update_status() method in the python file.The write method is used in this case 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 an extra date field date_status_last_updated, which will record the date the status was last updated.

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

The field value for date_status_last_updated must be updated by extending the update status() method. For that, define a method with the same name. This will help in extending an existing method. Then, all the operations specified in the parent class can be carried out using the super() method. Super is used to override a method that already exists in the same class. In Odoo, supering create() or write() methods are common. It is important to remember that all arguments to the extending function must also be specified. You must specify any additional parameters from the parent function in the inherited class as well. The extended method is shown below, which will execute all of the parent method's steps first, followed by the additional steps.

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

We can add steps to an existing function without affecting its functionality by overriding the existing method.

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