From 4dfc1e53f58fda432f1fef72c4ed9c32d5781da7 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Mon, 9 Jan 2012 15:04:32 +0100 Subject: [PATCH] [FIX] fields.o2m: respect static @domain when clearing a field (writing [(5,..)]) bzr revid: odo@openerp.com-20120109140432-3np49l2esds0l1yb --- openerp/osv/fields.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index ea43eb2fcdb..3a8448e22e5 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -510,11 +510,15 @@ class one2many(_column): elif act[0] == 5: reverse_rel = obj._all_columns.get(self._fields_id) assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed model does not have a m2o' - # if the model has on delete cascade, just delete the rows + # if the o2m has a static domain we must respect it when unlinking + extra_domain = self._domain if isinstance(getattr(self, '_domain', None), list) else [] + ids_to_unlink = obj.search(cr, user, [(self._fields_id,'=',id)] + extra_domain, context=context) + # If the model has cascade deletion, we delete the rows because it is the intended behavior, + # otherwise we only nullify the reverse foreign key column. if reverse_rel.column.ondelete == "cascade": - obj.unlink(cr, user, obj.search(cr, user, [(self._fields_id,'=',id)], context=context), context=context) + obj.unlink(cr, user, ids_to_unlink, context=context) else: - cr.execute('update '+_table+' set '+self._fields_id+'=null where '+self._fields_id+'=%s', (id,)) + obj.write(cr, user, ids_to_unlink, {self._fields_id: False}, context=context) elif act[0] == 6: # Must use write() to recompute parent_store structure if needed obj.write(cr, user, act[2], {self._fields_id:id}, context=context or {})