Blogs

Decorators in Odoo15

In Odoo, we have already seen how functions are defined and called. In addition to that, Odoo provides a way to extend its functionality, i.e, it allows us to modify the behavior of a function. For this purpose, Decorators in Odoo15 are used.

There are different types of decorators in Odoo. Some of them are:

  1. api.model
  2. api.depends
  3. api.onchange
  4. api.constrains
@api.model:

While working in Odoo15, in certain cases we need some functionality within the model itself without looking for the functionalities used in other models or records. Metadata information can be returned by using this decorator.

  • Used while migrating the codes.
  • Can be converted to a new API signature from old API calls.
  • Easy to upgrade write function and create function.

Automatic sequence generation can be done with the help of this decorator. We have to define a field for this in models,

Decorators in Odoo15

We are defining a method for this field,

Decorators in Odoo15

Here we have defined an automatic sequence generation method where sequence numbers get automatically generated based on the records created. Also, we have to define the record for the sequence generation.

Decorators in Odoo15

Here we can see that prefix is given as C and 4 digits are used. So when we create a record, we can automatically get the sequence number.

Decorators in Odoo15

C0008 is automatically generated.

@api.depends:

In Odoo, when a compute function is dependent on another record, then we have to use this depends decorator. This decorator is will be called when some change happens in the specified fields.

We can look at an example calculating the count of students and make it display in the tutor record.

Decorators in Odoo15

Decorators in Odoo15

The number of students will be calculated based on the domain provided.

Decorators in Odoo15

This is the student record and we can see this is the filtered view of students by their tutors. So let us see this same value is obtained in the tutor records.

Decorators in Odoo15

We can see that this tutor has 3 students, which was already shown in the student record view.

@api.onchange:

This decorator is used to cause changes in function when any changes are caused to the fields specified.

For example,

Consider two fields and we can see how values change using onchange.

Decorators in Odoo15

We are defining a function to pass values into the above fields,

Decorators in Odoo15

Below, the record is shown to which the method is passed. Here we can see that based on the course fields, the price and extra hour cost changes.

Decorators in Odoo15

@api.constrains:

This decorator will be called when a record is created and anything is written into the record as provided in the corresponding function. The function will be called whenever the field is altered and we can make validation for this field. While modifying the particular field in the record, in case the validation fails, an error message got printed.

Consider an example of a student model with library records,

Decorators in Odoo15

From the code, it is clear that if the number of books should be less than 4, otherwise it raises an error.

Decorators in Odoo15

We can see that for value 5, it shows the validation error and whenever it is greater, the same happens.

With the help of decorators, we saw that the function can be modified easily. In simple words, decorators can be used to extend the characteristics and behavior of a method and returns it.