Skip to content

Commit 5c8e54c

Browse files
Littlericketbb-Ricardo
authored andcommitted
fix: handle object and multi-object custom fields correctly
1 parent f496c95 commit 5c8e54c

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

module/netbox/object_classes.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,29 @@ def update(self, data=None, read_from_netbox=False, source=None):
683683
if self.data_model.get(key) == NBCustomField:
684684
if current_value is None:
685685
current_value = dict()
686+
687+
# Fix for object/multi-object custom fields
688+
# When patching, we only need the IDs, not the full object representation
689+
new_value_copy = new_value.copy()
690+
for field_name, field_value in new_value_copy.items():
691+
# Check for custom field type
692+
custom_field = self.inventory.get_by_data(NBCustomField, data={"name": field_name})
693+
if custom_field is not None:
694+
field_type = grab(custom_field, "data.type")
695+
696+
# Handle object type custom fields - need only ID
697+
if field_type == "object" and isinstance(field_value, dict) and field_value.get('id') is not None:
698+
new_value[field_name] = field_value.get('id')
699+
700+
# Handle multi-object type custom fields - need list of IDs
701+
elif field_type == "multi-object" and isinstance(field_value, list):
702+
ids = []
703+
for item in field_value:
704+
if isinstance(item, dict) and item.get('id') is not None:
705+
ids.append(item.get('id'))
706+
if ids:
707+
new_value[field_name] = ids
708+
686709
new_value = {**current_value, **new_value}
687710
new_value_str = str(new_value)
688711
elif isinstance(new_value, (NetBoxObject, NBObjectList)):
@@ -1288,7 +1311,7 @@ def __init__(self, *args, **kwargs):
12881311
"object_types": list,
12891312
# field name (object_types) for NetBox < 4.0.0
12901313
"content_types": list,
1291-
"type": ["text", "longtext", "integer", "boolean", "date", "url", "json", "select", "multiselect"],
1314+
"type": ["text", "longtext", "integer", "boolean", "date", "url", "json", "select", "multiselect", "object", "multi-object"],
12921315
"name": 50,
12931316
"label": 50,
12941317
"description": 200,

0 commit comments

Comments
 (0)