Odoo decorators

There are many decorators available in odoo. In this blog we will describe all the famous and most usable decorators available in Odoo



@api.model

This decorator is used to define a method that operates at the model level. It implies that the method does not require any particular record to be executed. It's commonly used for utility methods or for computations that don't depend on specific records.


@api.onchange

This decorator is used to trigger a method when certain fields are changed. It's often used to compute values dynamically based on user input.


@api.depends

This decorator is used to specify dependencies between fields. It tells Odoo to recompute the decorated field whenever any of the specified fields change. 




@api.constrains

This decorator is used to define constraints that must be satisfied by the records. It's typically used to validate data before it's saved to the database.






@api.model_create_multi

The @api.model_create_multi decorator in Odoo is used to mark a method as a handler for creating multiple records of a model in a batch. It's typically used when you want to optimize the creation of multiple records by processing them in a single database transaction, which can improve performance compared to creating records one by one.

Here's an example of how @api.model_create_multi decorator can be used:


In this example, the create method is decorated with @api.model_create_multi. When multiple records are created using the create method of this model, Odoo will batch the creation process and call this method with a list of dictionaries (vals_list), each representing the values for a new record. The method then creates the records and returns them.

Using @api.model_create_multi can help optimize the creation of multiple records, especially when dealing with large amounts of data, by reducing the overhead of database transactions and improving performance.


@api.ondelete

The ondelete decorator is used to mark a method/function which is to be called on unlinking the records. This is used to prevent the deletion of records, like lines on posted entries while uninstalling the module which cannot be obtained by overriding the unlink() method.

The methods with ondelete decorators check for some conditions specified in them and raise an error based on the result. The method should be named like either _unlink_if_<condition> or _unlink_except_<not_condition>.

at_uninstall (bool) – Whether the decorated method should be called if the module is being uninstalled. If False, the module uninstallation does not trigger those errors.

Here is an example code for @api.ondelete:


@api.returns



api.autovacuum

All the methods defined with the autovacuum method decorator will be invoked by the daily vacuum cron job defined with the model ir.autovacuum. The methods that perform garbage-collection-like tasks and need to be run daily can be defined with the autovacuum decorator. So that we don't have to create a dedicated cron job for it.

Here is an example code for @api.autovacuum:




Mahfujul Hasan

Software Engineer 

LinkedIn: https://www.linkedin.com/in/shojibhasan/


Comments