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.
- Create
- Write
- 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.Read More: How to Create and Use Classes in Python
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.
17 Comments
Thanku so much. Today you really save my time
ReplyDeleteThnx man reamy you save my life and my project
ReplyDeleteThank you but where is the read() method
ReplyDeleteHello Mosab Awad in odoo there is no read method.
Deletesuper(Campus,self).unlink()
ReplyDeleteCampus is the name of module or the class name
super(Campus,self).unlink()
DeleteIn above code "Campus" is the name of class.
Is it possible to update field by overriding create method?
ReplyDeleteYes, its possible. You can also override write method to do that.
ReplyDeleteyour tutorials are just awsome,,can u do for reorting too,i.e qweb reports in odoo,regards thanks
ReplyDeleteOdoo QWEB Reports
Delete1 - 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
hello , i need a function which inherit a view , any help
ReplyDeletewhy we use Super with class and self ?
ReplyDeleteCompus_create is name of an object to store the return values ?
pleas Answer i need to understand this
Answer 1: the result of a super call is to call the same method in the previously installed module that inherits the same model.
DeleteAnswer 2: Yes, campus_create have the id of created record
What does the default odoo write func do?
ReplyDeletewhenever 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
DeleteHi, im overriding the create method but it dos not work for me Im I doing something wrong?
ReplyDeleteI 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
Self not accessible in create method just loop through on "res" like this
Deletefor i in res: