kpg
kpg

Reputation: 7966

How to delete records in a different model in Odoo

I have added a Boolean attribute to res_config_settings. When the Boolean is False I want to delete all records in a custom Model ('my.device').

I have tried three different approaches:

1.In res_config_settings:

devices = self.env['my.device'].browse()
devices.unlink()
  1. also in res_config_settings:
devices = self.env['my.device'].browse()
for d in devices:
    d.unlink()
  1. in my.device model
def unlink_all(self):
    for rec in self:
        rec.unlink()

Then call self.env['pushbullet.device'].unlink_all() from res_config_settings. None of the options work but strangely, the first time I tried Option 1, all but one record was deleted.

Upvotes: 0

Views: 2264

Answers (1)

CZoellner
CZoellner

Reputation: 14768

Depends on two special factors: security and active mechanism. To really delete everything use sudo() to act as superuser and .with_context(active_test=False) to also find inactive records.

Example:

self.env['my.device'].sudo().with_context(active_test=False).search([]).unlink()

Or maybe more readable:

device_sudo = self.env['my.device'].sudo()
all_devices = device_sudo.with_context(active_test=False).search([])
all_devices.unlink()

If you don't want to bypass security/access rules, just remove the sudo parts.

Upvotes: 1

Related Questions