
Learn how to use the @api.ondelete decorator in Odoo to manage record deletions and control behavior during module uninstallation. Ensure data integrity!
In Odoo, handling the deletion of records while ensuring data integrity is a key part of developing robust applications. The @api.ondelete decorator is used in Odoo to define custom behavior when records are deleted, especially for cleanup or to enforce restrictions when a record is unlinked. This blog will provide an overview of how to use the @api.ondelete decorator in Odoo, along with an example.
The @api.ondelete decorator is used to trigger specific actions when a record is deleted. This decorator can be particularly useful when you want to ensure proper cleanup of related data, avoid orphan records, or perform specific logic when deleting a record.
It is primarily used for records that should trigger actions during uninstalling modules, to prevent data loss or corruption. The decorator allows you to specify whether the action should occur during module uninstallation by setting the parameter at_uninstall to True or False.
Here’s a basic example of how to implement the @api.ondelete decorator in Odoo:
from odoo import models, api
class CustomModel(models.Model):
_name = 'custom.model'
@api.ondelete(at_uninstall=False)
def _unlink_related_data(self):
# Custom logic to execute when record is deleted
related_records = self.env['related.model'].search([('custom_id', '=', self.id)])
related_records.unlink()
In this example, the @api.ondelete decorator is applied to the _unlink_related_data method. This method is called whenever a record from the custom.model is deleted. It searches for related records in the related.model and deletes them when the parent record is removed.
The at_uninstall=False parameter ensures that the method will not be executed during the uninstallation of the module. This is useful when you want to avoid deleting certain records during module removal.
The @api.ondelete decorator is ideal when:
For example, when working with many-to-many or one-to-many relationships, ensuring data consistency during deletions is crucial. Using @api.ondelete ensures that related records are properly handled.
By default, the @api.ondelete decorator does not trigger when the module is uninstalled unless at_uninstall is set to True. Here’s an example:
@api.ondelete(at_uninstall=True)
def _unlink_related_on_uninstall(self):
# Cleanup logic that runs on uninstall
self.env['related.model'].search([('custom_id', '=', self.id)]).unlink()
In this case, the method will be called both during record deletion and when the module is uninstalled, ensuring that data cleanup occurs even when uninstalling the module.
The @api.ondelete decorator in Odoo provides a powerful way to manage record deletions and related data handling. Whether you're ensuring proper cleanup, avoiding orphan records, or controlling behavior during module uninstallation, @api.ondelete gives you flexibility and control over how data is managed during record deletions.
For more advanced Odoo customization and best practices on using the @api.ondelete decorator, refer to the official Odoo documentation or consult experienced Odoo developers.
Your email address will not be published. Required fields are marked *