What is the use of ondelete parameter in odoo?

In Odoo, the ondelete parameter is used when defining relational fields (like Many2one, One2many, or Many2many). It tells Odoo what should happen to the related record(s) when the linked record is deleted.


Common ondelete options :​

cascade :

If the related record is deleted, also delete this record.

Example: Delete a sales order line when its sales order is deleted.


set null :

If the related record is deleted, set the field to NULL instead of deleting this record.

Example: If a responsible user is deleted, just remove the user reference but keep the task.


restrict :

Prevent deletion of the related record if it is still referenced.

Example: You can’t delete a product if it is used in sale orders.


set default :

If the related record is deleted, set the field to its default value.

Example: If a category is deleted, set the product’s category back to “ALL”.


# Cascade

order_id = fields.Many2one('sale.order', ondelete='cascade')

# Set null

user_id = fields.Many2one('res.users', ondelete='set null')

# Restrict

product_id = fields.Many2one('product.product', ondelete='restrict')