Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading