Chapter 4 - Odoo 15 Development Book

Onchange Methods

An onchange method is triggered when the value for a field id is changed in the user interface, usually in a form view. Let’s see this with the help of an example. For that, consider a model which stores all student records.

class Student(models.Model):
   _name = "student.student"
   _description = "Student"
   partner_id = fields.Many2one('res.partner', string="Partner")
   phone = fields.Char(string="Phone Number")
   email = fields.Char(string="Email", required=True)

Now, let us define an onchange method for the field partner_id which will update the email and phone field values. We can write the email and phone number for the student according to the partner selected in the partner_id field.

@api.onchange('partner_id')
def _onchange_partner_id(self):
   self.write({
       'email': self.partner_id.email,
       'phone': self.partner_id.phone
   })

An onchange method uses api decorator onchange() along with the field passed as parameter. Hence the method defined will be performed when that field value changes from the user interface. It is possible to pass more than one field as an argument for the onchange() decorator.

Init Hooks

Init hook from the manifest file can be used to perform any operation you want. This section shows how the init hook can be used to create some records. For that, we are using post_init_hook.

The model in which we are creating records is student.student

from odoo import fields, models
class Student(models.Model):
   _name = "student.student"
   _description = "Student"
   name = fields.Char(string="Name", required=True)
   phone = fields.Char(string="Phone Number")
   email = fields.Char(string="Email", required=True)

First step is to register the hook in the __manifest__.py file using the key. post_init_hook.after installing the module, Odoo will search for the corresponding method in the init file and will execute it.

'post_init_hook': 'create_student',

Add the create_student() method in init file.In this method, we are creating a sample student record using the create method. In real time scenario we can implement complex business logic here.

from odoo import api, SUPERUSER_ID
def create_student(cr, registry):
   env = api.Environment(cr, SUPERUSER_ID, {})
   env['student.student'].create({
       'name': 'Student 1',
       'phone': '+7865 6675 2341',
       'email': 'student1@student.com'
   })

Odoo also supports 2 other hooks called pre_init_hook hook and uninstall_hook. pre_init_hook will be invoked before the module installation, where as uninstall_hook is invoked at the time of module installation.

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