From dda56f21f3598234abd98706d6cf814dce230125 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 17 Apr 2023 14:39:39 -0700 Subject: [PATCH] #12256 remove read-only fields from writable serializers --- netbox/core/api/schema.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index e938c01cf8..1502592b06 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -151,8 +151,12 @@ def get_serializer_ref_name(self, serializer): def get_writable_class(self, serializer): properties = {} fields = {} if hasattr(serializer, 'child') else serializer.fields + remove_fields = [] for child_name, child in fields.items(): + # read_only fields don't need to be in writable (write only) serializers + if 'read_only' in dir(child) and child.read_only: + remove_fields.append(child_name) if isinstance(child, (ChoiceField, WritableNestedSerializer)): properties[child_name] = None elif isinstance(child, ManyRelatedField) and isinstance(child.child_relation, SerializedPKRelatedField): @@ -166,7 +170,12 @@ def get_writable_class(self, serializer): meta_class = getattr(type(serializer), 'Meta', None) if meta_class: ref_name = 'Writable' + self.get_serializer_ref_name(serializer) - writable_meta = type('Meta', (meta_class,), {'ref_name': ref_name}) + # remove read_only fields from write-only serializers + fields = list(meta_class.fields) + for field in remove_fields: + fields.remove(field) + writable_meta = type('Meta', (meta_class,), {'ref_name': ref_name, 'fields': fields}) + properties['Meta'] = writable_meta self.writable_serializers[type(serializer)] = type(writable_name, (type(serializer),), properties)