An Overview of Method Decorators
in Odoo 17
Enterprise
Introduction
Enterprise
In Odoo 17, decorators modify the behavior of methods within
models, controllers, and other components. The decorators are
used to add functionalities or modify the existing ones of the
function to which the decorator is added.
Steps
Enterprise
Here are some commonly used decorators in Odoo 17:
@api.model
In Odoo, the ‘@api.model’ decorator is indeed used for methods where the
‘self’ represents a record set, but only the model itself is relevant, not its
records. This decorator is particularly useful during code migration from older
versions of Odoo to newer ones.
Steps
Enterprise
Steps
Enterprise
@api.autovacuum
The ‘ @api.autovacuum’ decorator in Odoo schedules methods to run with the
daily vacuum cron job (ir.autovacuum). It's ideal for executing garbage
collection-like tasks that require daily cleanup without needing a dedicated
cron job entry. This decorator simplifies maintenance and ensures database
optimization by automating routine cleanup operations.
Enterprise
Steps
Enterprise
@api.constrains
The ‘@api.constrains’ decorator in Odoo specifies constraint dependencies for a
method, triggering it only when certain specified fields are included in the
creating or writing call, and if they are present in the view. It supports only
simple field names and ignores dotted names. Typically, this decorator is used
to raise a ValidationError if the check fails.
Enterprise
Steps
Enterprise
@api.depends
The ‘@api.depends’ decorator in Odoo is used to specify field dependencies for
computed fields. It informs Odoo that the computed field's value depends on
the values of other fields, ensuring that the computed field is recalculated
whenever any of its dependencies change.
Enterprise
This function is recomputed whenever there is a change in the fields ‘cost_price’ and ‘sale_price’.
Steps
Enterprise
@api.model_create_multi
The Odoo @api.multi decorator creates multiple records from a single or a list
of dictionaries. It's useful for bulk record creation, improving efficiency and
productivity. The values are conveyed as parameters to the method.
Steps
Enterprise
Steps
Enterprise
@api.onchange
The ‘@api.onchange’ decorator in Odoo automatically triggers methods in
response to changes in specified fields within form views. By marking a
method with this decorator within a model, these methods are invoked
whenever the designated fields are modified, facilitating dynamic updates and
actions based on user input.
Enterprise
Steps
Enterprise
@api.ondelete
The ‘@api.ondelete’ decorator in Odoo marks a method to be called when
records are being unlinked (deleted). It's often used to prevent the deletion of
records, such as lines on posted entries, during module uninstallation, which
cannot be achieved by overriding the unlink() method.
Parameters:
* at_uninstall: A boolean parameter indicating whether the decorated method
should be called during module uninstallation. If False, errors raised by the
method won't prevent module uninstallation.
Enterprise
Steps
Enterprise
@api.returns
The ‘@api.returns’ decorator is used to specify the return type of methods that
return instances of a particular model. It adapts the method output to the
Odoo API style, either traditional style (returning ids or False) or record style
(returning recordset).
Parameters:
* model: Specifies the model name or 'self' for the current model.
* downgrade: Optional function to convert the record-style output to
traditional-style output.
* upgrade: Optional function to convert the traditional-style output to record-
style output.
Enterprise
Conclusion
Enterprise
In conclusion, decorators serve various purposes, such as enhancing database
maintenance, ensuring data integrity, and facilitating dynamic behavior in
response to user actions. Understanding and leveraging these decorators is
essential for efficient and effective Odoo development.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

An Overview of Method Decorators in Odoo 17

  • 1.
    An Overview ofMethod Decorators in Odoo 17 Enterprise
  • 2.
    Introduction Enterprise In Odoo 17,decorators modify the behavior of methods within models, controllers, and other components. The decorators are used to add functionalities or modify the existing ones of the function to which the decorator is added.
  • 3.
    Steps Enterprise Here are somecommonly used decorators in Odoo 17: @api.model In Odoo, the ‘@api.model’ decorator is indeed used for methods where the ‘self’ represents a record set, but only the model itself is relevant, not its records. This decorator is particularly useful during code migration from older versions of Odoo to newer ones.
  • 4.
  • 5.
    Steps Enterprise @api.autovacuum The ‘ @api.autovacuum’decorator in Odoo schedules methods to run with the daily vacuum cron job (ir.autovacuum). It's ideal for executing garbage collection-like tasks that require daily cleanup without needing a dedicated cron job entry. This decorator simplifies maintenance and ensures database optimization by automating routine cleanup operations.
  • 6.
  • 7.
    Steps Enterprise @api.constrains The ‘@api.constrains’ decoratorin Odoo specifies constraint dependencies for a method, triggering it only when certain specified fields are included in the creating or writing call, and if they are present in the view. It supports only simple field names and ignores dotted names. Typically, this decorator is used to raise a ValidationError if the check fails.
  • 8.
  • 9.
    Steps Enterprise @api.depends The ‘@api.depends’ decoratorin Odoo is used to specify field dependencies for computed fields. It informs Odoo that the computed field's value depends on the values of other fields, ensuring that the computed field is recalculated whenever any of its dependencies change.
  • 10.
    Enterprise This function isrecomputed whenever there is a change in the fields ‘cost_price’ and ‘sale_price’.
  • 11.
    Steps Enterprise @api.model_create_multi The Odoo @api.multidecorator creates multiple records from a single or a list of dictionaries. It's useful for bulk record creation, improving efficiency and productivity. The values are conveyed as parameters to the method.
  • 12.
  • 13.
    Steps Enterprise @api.onchange The ‘@api.onchange’ decoratorin Odoo automatically triggers methods in response to changes in specified fields within form views. By marking a method with this decorator within a model, these methods are invoked whenever the designated fields are modified, facilitating dynamic updates and actions based on user input.
  • 14.
  • 15.
    Steps Enterprise @api.ondelete The ‘@api.ondelete’ decoratorin Odoo marks a method to be called when records are being unlinked (deleted). It's often used to prevent the deletion of records, such as lines on posted entries, during module uninstallation, which cannot be achieved by overriding the unlink() method. Parameters: * at_uninstall: A boolean parameter indicating whether the decorated method should be called during module uninstallation. If False, errors raised by the method won't prevent module uninstallation.
  • 16.
  • 17.
    Steps Enterprise @api.returns The ‘@api.returns’ decoratoris used to specify the return type of methods that return instances of a particular model. It adapts the method output to the Odoo API style, either traditional style (returning ids or False) or record style (returning recordset). Parameters: * model: Specifies the model name or 'self' for the current model. * downgrade: Optional function to convert the record-style output to traditional-style output. * upgrade: Optional function to convert the traditional-style output to record- style output.
  • 18.
  • 19.
    Conclusion Enterprise In conclusion, decoratorsserve various purposes, such as enhancing database maintenance, ensuring data integrity, and facilitating dynamic behavior in response to user actions. Understanding and leveraging these decorators is essential for efficient and effective Odoo development.
  • 20.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com