1
1
# -*- coding: utf-8 -*-
2
+ """
3
+ Utility functions for modifying model fields.
4
+
5
+ Field operations are best done in `pre-` script of the involved modules. In some cases a
6
+ preliminary operation could be done in pre and finished in post. A common example is to
7
+ remove a field in pre- keeping its column, later used in post when the column is finally
8
+ dropped.
9
+ """
10
+
2
11
import base64
3
12
import json
4
13
import logging
@@ -73,10 +82,13 @@ def make_index_name(table_name, column_name):
73
82
def ensure_m2o_func_field_data (cr , src_table , column , dst_table ):
74
83
"""
75
84
Fix broken m2o relations.
85
+
76
86
If any `column` not present in `dst_table`, remove column from `src_table` in
77
87
order to force recomputation of the function field
78
88
79
89
WARN: only call this method on m2o function/related fields!!
90
+
91
+ :meta private: exclude from online docs
80
92
"""
81
93
if not column_exists (cr , src_table , column ):
82
94
return
@@ -92,6 +104,20 @@ def ensure_m2o_func_field_data(cr, src_table, column, dst_table):
92
104
93
105
94
106
def remove_field (cr , model , fieldname , cascade = False , drop_column = True , skip_inherit = ()):
107
+ """
108
+ Remove a field and its references from the database.
109
+
110
+ This function also removes the field from inheriting models, unless exceptions are
111
+ specified in `skip_inherit`. When the field is stored we can choose to not drop the
112
+ column.
113
+
114
+ :param str model: model name of the field to remove
115
+ :param str fieldname: name of the field to remove
116
+ :param bool cascade: whether the field column(s) are removed in `CASCADE` mode
117
+ :param bool drop_column: whether the field's column is dropped
118
+ :param list(str) or str skip_inherit: list of inheriting models to skip the removal
119
+ of the field, use `"*"` to skip all
120
+ """
95
121
_validate_model (model )
96
122
97
123
ENVIRON ["__renamed_fields" ][model ][fieldname ] = None
@@ -339,11 +365,15 @@ def adapter(leaf, is_or, negated):
339
365
340
366
def remove_field_metadata (cr , model , fieldname , skip_inherit = ()):
341
367
"""
368
+ Remove metadata of a field.
369
+
342
370
Due to a bug of the ORM [1], mixins doesn't create/register xmlids for fields created in children models
343
371
Thus, when a field is no more defined in a child model, their xmlids should be removed explicitly to
344
372
avoid the fields to be considered as missing and being removed at the end of the upgrade.
345
373
346
374
[1] https://github.com/odoo/odoo/issues/49354
375
+
376
+ :meta private: exclude from online docs
347
377
"""
348
378
_validate_model (model )
349
379
@@ -360,6 +390,20 @@ def remove_field_metadata(cr, model, fieldname, skip_inherit=()):
360
390
361
391
362
392
def move_field_to_module (cr , model , fieldname , old_module , new_module , skip_inherit = ()):
393
+ """
394
+ Move a field from one module to another.
395
+
396
+ This functions updates all references to a specific field, switching from the source
397
+ module to the destination module. It avoid data removal after the registry is fully
398
+ loaded. The field in inheriting models are also moved unless skipped.
399
+
400
+ :param str model: name of the owner model of the field to move
401
+ :param str fieldname: name of the field to move
402
+ :param str old_module: source module name from which the field is moved
403
+ :param str new_module: target module name into which the field is moved
404
+ :param list(str) or str skip_inherit: list of inheriting models for which the field is
405
+ not to be moved, use `"*"` to skip all
406
+ """
363
407
_validate_model (model )
364
408
name = IMD_FIELD_PATTERN % (model .replace ("." , "_" ), fieldname )
365
409
try :
@@ -385,6 +429,25 @@ def move_field_to_module(cr, model, fieldname, old_module, new_module, skip_inhe
385
429
386
430
387
431
def rename_field (cr , model , old , new , update_references = True , domain_adapter = None , skip_inherit = ()):
432
+ """
433
+ Rename a field and its references from `old` to `new` on the given `model` and all inheriting models, unless exceptions are specified in `skip_inherit`.
434
+
435
+ This functions also updates references, indirect or direct ones, including filters,
436
+ server actions, related fields, emails, dashboards, domains, and many more. See
437
+ :func:`update_field_usage`
438
+
439
+ For the update of domains a special adapter function can be used. The default adapter
440
+ just replaces `old` by `new` in each domain leaf. Refer to :func:`adapt_domains` for
441
+ information about domain adapters.
442
+
443
+ :param str model: model name of the field to rename
444
+ :param str old: current name of the field to rename
445
+ :param str new: new name of the field to rename
446
+ :param bool update_references: whether to update all references
447
+ :param function domain_adapter: adapter to use for domains, see :func:`adapt_domains`
448
+ :param list(str) or str skip_inherit: models to skip when renaming the field in
449
+ inheriting models, use `"*"` to skip all
450
+ """
388
451
_validate_model (model )
389
452
390
453
rf = ENVIRON ["__renamed_fields" ][model ]
@@ -565,10 +628,16 @@ def convert_field_to_property(
565
628
cr , model , field , type , target_model = None , default_value = None , default_value_ref = None , company_field = "company_id"
566
629
):
567
630
"""
568
- Notes:
631
+ Convert a field to a property field.
632
+
633
+ Notes
634
+ -----
569
635
`target_model` is only use when `type` is "many2one".
570
636
The `company_field` can be an sql expression.
571
637
You may use `t` to refer the model's table.
638
+
639
+ :meta private: exclude from online docs
640
+
572
641
"""
573
642
_validate_model (model )
574
643
if target_model :
@@ -763,6 +832,19 @@ def convert_field_to_untranslatable(cr, model, field, type="varchar"):
763
832
764
833
765
834
def change_field_selection_values (cr , model , field , mapping , skip_inherit = ()):
835
+ """
836
+ Replace references of values of a selection field.
837
+
838
+ This function replaces all references to selection values according to a mapping.
839
+ Domains are also updated.
840
+
841
+ :param str model: model name of the selection field to update
842
+ :param str field: name of the selection field to update
843
+ :param dict mapping: selection values to update, key values are replaced by
844
+ their corresponding values in the mapping
845
+ :param list(str) or str skip_inherit: list of inheriting models to skip in the update
846
+ of the selection values, use `"*"` to skip all
847
+ """
766
848
_validate_model (model )
767
849
if not mapping :
768
850
return
@@ -837,7 +919,9 @@ def register_unanonymization_query(cr, model, field, query, query_type="sql", se
837
919
838
920
def update_field_usage (cr , model , old , new , domain_adapter = None , skip_inherit = ()):
839
921
"""
840
- Replace all references to field `old` to `new` in:
922
+ Replace all references to the field `old` by `new` in different places.
923
+
924
+ Search in:
841
925
- ir_filters
842
926
- ir_exports_line
843
927
- ir_act_server
@@ -846,6 +930,17 @@ def update_field_usage(cr, model, old, new, domain_adapter=None, skip_inherit=()
846
930
- domains (using `domain_adapter`)
847
931
- related fields
848
932
933
+ This function can be used to replace the usage of a field by another. Domains are
934
+ updated using the `domain_adapter`. By default the domain adapter just replaces `old`
935
+ by `new` in domain leaves. See :func:`adapt_domains` for more information about domain
936
+ adapters.
937
+
938
+ :param str model: model name of the field
939
+ :param str old: source name of the field to replace
940
+ :param str new: target name of the field to set
941
+ :param function domain_adapter: adapter to use for domains, see :func:`adapt_domains`
942
+ :param list(str) or str skip_inherit: models to skip when renaming the field in
943
+ inheriting models, use `"*"` to skip all
849
944
"""
850
945
return _update_field_usage_multi (cr , [model ], old , new , domain_adapter = domain_adapter , skip_inherit = skip_inherit )
851
946
@@ -1140,6 +1235,8 @@ def adapt_related(cr, model, old, new, skip_inherit=()):
1140
1235
1141
1236
def update_server_actions_fields (cr , src_model , dst_model = None , fields_mapping = None ):
1142
1237
"""
1238
+ Update server action fields.
1239
+
1143
1240
When some fields of `src_model` have ben copied to `dst_model` and/or have
1144
1241
been copied to fields with another name, some references have to be moved.
1145
1242
@@ -1149,6 +1246,8 @@ def update_server_actions_fields(cr, src_model, dst_model=None, fields_mapping=N
1149
1246
is given, the `src_model` is used as `dst_model`.
1150
1247
Then, if `dst_model` is set, `ir_act_server` referred by modified `ir_server_object_lines`
1151
1248
are also updated. A chatter message informs the customer about this modification.
1249
+
1250
+ :meta private: exclude from online docs
1152
1251
"""
1153
1252
if dst_model is None and fields_mapping is None :
1154
1253
raise SleepyDeveloperError (
0 commit comments