Hey Cyllo community! Need some help with `@api.ondelete` – how do I properly set up cascading deletes? Any tips or examples would be awesome!
Welcome!
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.
This question has been flagged
Here's how you can handle custom delete behavior in Odoo using the '@api.ondelete' decorator. This example prevents deletion of sale order lines with quantities exceeding 10. from odoo import models, fields, api from odoo.exceptions import UserError class SaleOrderLine(models.Model): _inherit = 'sale.order.line' @api.ondelete(at_uninstall=False) def _check_line_deletion(self): """ This method runs whenever a record is deleted. If the line has a quantity greater than 10, prevent deletion. """ for line in self: if line.product_uom_qty > 10: raise UserError( f"Cannot delete line with quantity greater than 10 (Line ID: {line.id})" )