How to override create method in odoo API 8 ?

Hello everyone. In this blog-post tutorial, we will learn how to override create method on odoo using API8.
The create method is invoked everytime we click on the “NEW” button and when we save through the “SAVE” button.
Let say we have a ‘Students’ model with “name” and “age” fields, we want to create a new student’s record with values like name=’Adam’ age=’10’.

Here we have ovrriden the create method of Students model.

create

Explanation of code

  • To instantiate a model we must inherit an openerp model or odoo model in odoo version 10.
  • Create method execute an SQL INSERT query on the table / model,
  • @api.model decorate a record-style method where self is a recordset, it used for create method.
  • self here indicate the model in which the record will be created.
  • vals is a dictionary containing the values to store in the new record. The dictionary elements are in the form {‘field_name’:’field_value’}.
  • The vals dictionary can be modified before calling the super method.
  • Here res will hold the browse record of created record.

Make sure that you use the exact name of the python class in the super function and also that you return the same object you get from it.

Resume

The create method creates new records. It takes a dictionary of values, applies any defaults we have specified and then inserts the record an returns the new ID.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.