Community mailing list archives

community@mail.odoo.com

Unlink and ondelete="cascade"

by
Andre Kurniawan
- 03/03/2016 04:58:33

Hello Community,

Please take a look at this snippet:

from openerp import models, fields, api

class ModelParent(models.Model):
    _name = "my_module.model_parent"

    name = fields.Char("Parent Name", required=True)

class ModelChild(models.Model):
    _name = "my_module.model_child"

    name = fields.Char("Child Name", required=True)

    parent_id = fields.Many2one("my_module.model_parent", required=True, ondelete="cascade")

    @api.multi
    def unlink(self):
        # do something here
        # This method only called when this record deleted directly, not if `ModelParent` is deleted

        return super(ModelChild, self).unlink()

The unlink method of ModelChild is not called when it’s parent (ModelParent) is deleted. Does ondelete="cascade" only delete the ModelChild records via database query and not from unlink method?

-- 
Regards,
Andre