diff --git a/src/util/fields.py b/src/util/fields.py index ee66fda1f..1e6c3295b 100644 --- a/src/util/fields.py +++ b/src/util/fields.py @@ -190,7 +190,23 @@ def filter_value(key, value): return changed -def remove_field(cr, model, fieldname, cascade=False, drop_column=True, skip_inherit=(), keep_as_attachments=False): +def _rm_field_adapter(leaf, is_or, negated): + # replace by TRUE_LEAF, unless negated or in a OR operation but not negated + if is_or ^ negated: + return [FALSE_LEAF] + return [TRUE_LEAF] + + +def remove_field( + cr, + model, + fieldname, + cascade=False, + drop_column=True, + skip_inherit=(), + keep_as_attachments=False, + update_references=True, +): """ Remove a field and its references from the database. @@ -206,6 +222,7 @@ def remove_field(cr, model, fieldname, cascade=False, drop_column=True, skip_inh of the field, use `"*"` to skip all :param bool keep_as_attachments: for binary fields, whether the data should be kept as attachments + :param bool update_references: whether to update all references """ _validate_model(model) @@ -225,23 +242,20 @@ def remove_field(cr, model, fieldname, cascade=False, drop_column=True, skip_inh _remove_import_export_paths(cr, model, fieldname) - def adapter(leaf, is_or, negated): - # replace by TRUE_LEAF, unless negated or in a OR operation but not negated - if is_or ^ negated: - return [FALSE_LEAF] - return [TRUE_LEAF] - - related = None - if column_exists(cr, "ir_model_fields", "related"): - cr.execute("SELECT related FROM ir_model_fields WHERE model=%s AND name=%s", [model, fieldname]) - if cr.rowcount: - related = cr.fetchone()[0] - - if related: - update_field_usage(cr, model, fieldname, related, skip_inherit=skip_inherit) - else: - # clean domains - adapt_domains(cr, model, fieldname, "ignored", adapter=adapter, skip_inherit=skip_inherit, force_adapt=True) + if update_references: + related = None + if column_exists(cr, "ir_model_fields", "related"): + cr.execute("SELECT related FROM ir_model_fields WHERE model=%s AND name=%s", [model, fieldname]) + if cr.rowcount: + related = cr.fetchone()[0] + + if related: + update_field_usage(cr, model, fieldname, related, skip_inherit=skip_inherit) + else: + # clean domains + adapt_domains( + cr, model, fieldname, "ignored", adapter=_rm_field_adapter, skip_inherit=skip_inherit, force_adapt=True + ) if table_exists(cr, "ir_server_object_lines"): cr.execute( @@ -441,6 +455,7 @@ def adapter(leaf, is_or, negated): drop_column=drop_column, skip_inherit=skip_inherit, keep_as_attachments=keep_as_attachments, + update_references=update_references, )