diff options
| author | Xavier ALT <xal@odoo.com> | 2019-09-11 21:15:34 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2019-09-20 12:23:09 +0000 |
| commit | c5fa4771fcec91c06a26d5103126a1373f962f0b (patch) | |
| tree | aa5c556e8812632e47b6146aaa0d58194bc48846 /odoo/fields.py | |
| parent | 1a3dcfeae658bf3d6411662cbb2c8c340c34f07f (diff) | |
[FIX] fields: default to ondelete='cascade' for required m2o fields on transient
TransientModel (wizards) can be annoying because if they have required
many2one fields, those fields will default to `ondelete='restrict'`,
preventing user to delete the comodel records without deleting the
transient model records first.
To improve such case, we default to `ondelete='cascade'` for required
many2one field on a TransientModel (unless specified otherwise)
closes odoo/odoo#36738
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Diffstat (limited to 'odoo/fields.py')
| -rw-r--r-- | odoo/fields.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/odoo/fields.py b/odoo/fields.py index 2576fd770ac..f3167b3ce77 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -2306,7 +2306,14 @@ class Many2one(_Relational): # 3) The ondelete attribute is explicitly defined as 'set null' for a required m2o, # this is considered a programming error. if not self.ondelete: - self.ondelete = 'restrict' if self.required else 'set null' + comodel = model.env[self.comodel_name] + if model.is_transient() and not comodel.is_transient(): + # Many2one relations from TransientModel Model are annoying because + # they can block deletion due to foreign keys. So unless stated + # otherwise, we default them to ondelete='cascade'. + self.ondelete = 'cascade' if self.required else 'set null' + else: + self.ondelete = 'restrict' if self.required else 'set null' if self.ondelete == 'set null' and self.required: raise ValueError( "The m2o field %s of model %s is required but declares its ondelete policy " @@ -2318,11 +2325,6 @@ class Many2one(_Relational): comodel = model.env[self.comodel_name] if not model.is_transient() and comodel.is_transient(): raise ValueError('Many2one %s from Model to TransientModel is forbidden' % self) - if model.is_transient() and not comodel.is_transient(): - # Many2one relations from TransientModel Model are annoying because - # they can block deletion due to foreign keys. So unless stated - # otherwise, we default them to ondelete='cascade'. - self.ondelete = self.ondelete or 'cascade' return super(Many2one, self).update_db(model, columns) def update_db_column(self, model, column): |
