Search Here

How to Override Create, Write and Unlink function

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.


Why Override Create, Write and Unlink Method in Odoo


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.

Read More: How to Create and Use Function in Python

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

How to Override 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.

Read More: How to Remove Save Button From Form View in Odoo

override_create_function.py



override_write_function.py



override_unlink_function.py


Code Description:

In above code we have a "your_model" class and we have override your_model class create, write or unlink method. 

What is @api


@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 "your_model" 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.

Post a Comment

17 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
  4. super(Campus,self).unlink()
    Campus is the name of module or the class name

    ReplyDelete
    Replies
    1. super(Campus,self).unlink()
      In above code "Campus" is the name of class.

      Delete
  5. Is it possible to update field by overriding create method?

    ReplyDelete
  6. Yes, its possible. You can also override write method to do that.

    ReplyDelete
  7. your tutorials are just awsome,,can u do for reorting too,i.e qweb reports in odoo,regards thanks

    ReplyDelete
    Replies
    1. Odoo QWEB Reports
      1 - http://learnopenerp.blogspot.com/2016/09/how-to-create-custom-reports-in-odoo.html
      2 - http://learnopenerp.blogspot.com/2016/11/how-to-create-qweb-reports-in-openerp.html

      Hope above tips will helps you

      Delete
  8. hello , i need a function which inherit a view , any help

    ReplyDelete
  9. why we use Super with class and self ?
    Compus_create is name of an object to store the return values ?
    pleas Answer i need to understand this

    ReplyDelete
    Replies
    1. Answer 1: the result of a super call is to call the same method in the previously installed module that inherits the same model.
      Answer 2: Yes, campus_create have the id of created record

      Delete
  10. What does the default odoo write func do?

    ReplyDelete
    Replies
    1. whenever you update the record, the default odoo write function called. If you want to do something special on write or update the records you should override the write function of odoo

      Delete
  11. Hi, im overriding the create method but it dos not work for me Im I doing something wrong?
    I need to write e value in antoher model when i create this one this is my code...

    @api.model
    def create(self,vals):
    res=super(Nota,self).create(vals)
    for i in self:
    for p in i.productos:
    if p.cantidad <= p.inventario:
    prod=p.producto_nota.tipo.nombre
    nuevo = p.inventario - p.cantidad
    a=self.env['producto'].search([('nombre', '=', prod)])
    a.write ({'inventario':nuevo,})
    print('Passed this function')
    return res

    ReplyDelete
    Replies
    1. Self not accessible in create method just loop through on "res" like this
      for i in res:

      Delete