Andrius
Andrius

Reputation: 21158

Odoo restrict delete of record that is still referenced (m2m)

I have this field:

range_application_ids = fields.Many2many('insurance.commission.rule.range.application', 'application_rule_range_rel', 'rule_range_id',
    'application_id', 'Applications', ondelete='restrict', required=True)

I need to restricts delete of that models (insurance.commission.rule.range.application) records if it is referenced through Many2many relation. Now if I go to that models records list and delete any of it, Odoo won't throw any warning and lets me do it. Then when I got to another models record that has relation with insurance.commission.rule.range.application through many2many field, I see that it was removed (and that field is required). Setting ondelete='restrict' did not do anything.

Is there a way to restrict such deletes with Odoo standard functionality or I need to implement such checking myself?

Upvotes: 1

Views: 3757

Answers (1)

Andrius
Andrius

Reputation: 21158

Now I implemented such constraint myself, but if anyone know how to do it using standard methods, please post another answer. Here is the code (it goes in insurance.commission.rule.range.application model (or class in other words):

@api.multi
def unlink(self):
    range_obj = self.env['insurance.commission.rule.range']
    rule_ranges = range_obj.search([('range_application_ids', 'in', self.ids)])
    if rule_ranges:
        raise Warning(_("You are trying to delete a record that is still referenced!"))
    return super(insurance_commission_rule_range_application, self).unlink()

Upvotes: 3

Related Questions