Decorators and Its Utility in Odoo?

Decorators and Its Utility in Odoo?

A decorator is a function that takes a function as input and returns a function.

The anatomy of the decorator is: @decorator-expression

Function-definition

Decorator-expression is an expression that returns a callable thing. The callable thing has to take a single argument. Function definition is a function definition (it, therefore, must begin with def).

In older version of odoo, we use a cursor, uid, context and ids for defining all method.

In odoo 9, odoo 10 we use self as the parameter when defining a method with certain method decorators.This decorator are passed the parameters explicitly to the method. Which provide a smooth way to write functions and avoid complications.

Decorators in odoo:

  1. @api.one
  2. @api.multi
  3. @api.model
  4. @api.depends
  5. @api.onchange
  6. @api.constrains

1.@api.one:

It is specific for one record and cannot be used for multiple records. Self is redefined as current record.

Example

@api.one
def get_product(self):

self.product_name = ‘desktop’

This function will automatically write product name field of each record with ‘desktop’.

2.@api.multi:

                        It can use for multiple records, so we can loop through it. Here Self-contains recordset so that we need for loop for iterating through each record.

Example

@api.multi
def get_product(self):

For the record in self:

self.product_name = ‘desktop’

3.@api.model:

                        This decorator will convert old API calls to decorated function to new API signature.

                       

4.@api.depends:

This decorator is specifically used for computing fields in odoo. The decorator function works only if any of the fields specified in the @api.depends is changed in the form.

Dependencies can be dotted paths when using sub-fields.

Example:

@api.depends(‘partner_id.street’)

def _get_street(self):

for record in self:

for line in record.partner_id:

record.street = line.street

Computed field are not stored by default, they have computed the function and return the value.We use store = True to store the computed value to the database.

  1. @api.onchange:

This decorator will avoid the on change attribute in the xml file. The fields are written as the parameter of the function.

Example:

@api.onchange(‘date_of_birth’,’age’)

def onchange_age_calculation(self ):

if not self.date_of_birth:

self.age = 0

else:

today_date = datetime.now().year

date_split = self.date_of_birth.split(‘-‘,1)

birth_year = date_split[0]

self.age = today_date -int(birth_year)

 

This function will automatically write age field when changing the value of date_of_birth field in form view.

6.@api.constrains:

This decorator will ensure that decorated function will be called on creating, write, unlink operation. It will act as a constraint checker. This will especially use for warning messages and validation.

For Customized ERP Solutions at best prices Contact Us now