33
33
)
34
34
from .indirect_references import indirect_references
35
35
from .inherit import direct_inherit_parents , for_each_inherit
36
- from .misc import chunks , parse_version , version_gte
36
+ from .misc import Sentinel , chunks , parse_version , version_gte
37
37
from .orm import env , flush
38
38
from .pg import (
39
39
PGRegexp ,
@@ -1006,11 +1006,14 @@ def ensure_xmlid_match_record(cr, xmlid, model, values):
1006
1006
return new_res_id
1007
1007
1008
1008
1009
+ AUTOMATIC = Sentinel ("AUTOMATIC" )
1010
+
1011
+
1009
1012
def update_record_from_xml (
1010
1013
cr ,
1011
1014
xmlid ,
1012
1015
reset_write_metadata = True ,
1013
- force_create = True ,
1016
+ force_create = AUTOMATIC ,
1014
1017
from_module = None ,
1015
1018
reset_translations = (),
1016
1019
ensure_references = False ,
@@ -1028,15 +1031,17 @@ def update_record_from_xml(
1028
1031
1029
1032
:param str xmlid: record xml_id, under the format `module.name`
1030
1033
:param bool reset_write_metadata: whether to update the `write_date` of the record
1031
- :param bool force_create: whether the record is created if it does not exist
1034
+ :param bool force_create: whether the record is created if it does not exist.
1035
+ `True` by default, unless `fields` is not None.
1032
1036
:param str from_module: name of the module from which to update the record, necessary
1033
1037
only when the specs are in a different module than the one in
1034
1038
the xml_id
1035
1039
:param set(str) reset_translations: field names whose translations get reset
1036
1040
:param bool ensure_references: whether referred records via `ref` XML attributes
1037
1041
should also be updated.
1038
1042
:param set(str) or None fields: optional list of fields to include in the XML declaration.
1039
- If set, all other fields will be ignored.
1043
+ If set, all other fields will be ignored. When set, record
1044
+ won't be created if missing.
1040
1045
1041
1046
.. warning::
1042
1047
This functions uses the ORM, therefore it can only be used after **all** models
@@ -1045,7 +1050,8 @@ def update_record_from_xml(
1045
1050
1046
1051
.. note::
1047
1052
The standard behavior of ORM is to create the record if it doesn't exist, including
1048
- its xml_id. That will happen on this function as well.
1053
+ its xml_id. That will happen on this function as well, unless `force_create` is set
1054
+ to `False`.
1049
1055
"""
1050
1056
__update_record_from_xml (
1051
1057
cr ,
@@ -1076,6 +1082,9 @@ def __update_record_from_xml(
1076
1082
if "." not in xmlid :
1077
1083
raise ValueError ("Please use fully qualified name <module>.<name>" )
1078
1084
1085
+ if force_create is AUTOMATIC :
1086
+ force_create = fields is None # don't force record creation if we are filtering fields
1087
+
1079
1088
module , _ , name = xmlid .partition ("." )
1080
1089
1081
1090
cr .execute (
0 commit comments