Tuesday, 21 November 2017

How to override Odoo functions

In this article you will learn how to override Odoo methods. I will show you how to override Odoo Create, Write and Unlink method. Here the question is why we need to override these (create, write and unlink) methods.


The answer is to add or remove additional functionality we use overriding. For example if I want to check some constraints before creating, editing or deleting our record than we may use Odoo override methods.

There are basically three methods which we can override.
  1. Create
  2. Write
  3. Unlink

Create Method

The create method invoke every time when we click on "Create" or "New" button on Odoo form view and when we save the from using Save button.

Below is the code snippet to override create method.

class Campus(models.Model):
    _name='campus'    
    @api.model
    def create(self,values):
        campus_create = super(Campus,self).create(values)
        return campus_create

Below is the code snippet to override write method.

class Campus(models.Model):
    _name='campus'    
    @api.multi
    def write(self,values):
        campus_write = super(Campus,self).write(values)
        return campus_write

Below is the code snippet to override unlink method.

class Campus(models.Model):
    _name='campus'    
    @api.multi
    def unlink(self,values):
        campus_unlink = super(Campus,self).unlink()
        return campus_unlink

Code Description:

In above code we have a "Campus" class and we override campus class create, write or unlink method. @api.model is a decorator that decorate a record style method. Notice here to override you should call super() method. Super method takes two parameter the first one is the name of class which is "Campus" and the second one is "self". Where self is a record set in which model the record is created. Values are dictionary that contains data which we are going to create, write or unlink.

4 comments:

  1. Thanku so much. Today you really save my time

    ReplyDelete
  2. Thnx man reamy you save my life and my project

    ReplyDelete
  3. Thank you but where is the read() method

    ReplyDelete
    Replies
    1. Hello Mosab Awad in odoo there is no read method.

      Delete