Community mailing list archives

Re: Unlink and ondelete="cascade"

Pedro M. Baeza
- 03/03/2016 05:28:27
Exactly, the ondelete is set at Postgres level, so you should take care of any custom unlink code in cascade models.


2016-03-03 11:07 GMT+01:00 vak0160 <>:

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")

    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?


Post to: