From a48d0046a99968f0e6824c8559af76964b42810d Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 15 Jun 2023 13:14:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=87=AA=E5=AE=9A=E4=B9=89=20field=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20required?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/serializers/dynamic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/common/serializers/dynamic.py b/apps/common/serializers/dynamic.py index 2a8ffe0c0..cac9ae0dc 100644 --- a/apps/common/serializers/dynamic.py +++ b/apps/common/serializers/dynamic.py @@ -49,10 +49,15 @@ def create_serializer_class(serializer_name, fields_info): for i, field_info in enumerate(fields_info): data = {k: field_info.get(k) for k in fields_name} field_type = data.pop('type', 'str') + + if data.get('default') is None: + data.pop('default', None) + data['required'] = field_info.get('required', True) data = set_default_by_type(field_type, data, field_info) data = set_default_if_need(data, i) + if data.get('default', None) is not None: + data['required'] = False field_name = data.pop('name') field_class = type_field_map.get(field_type, serializers.CharField) serializer_fields[field_name] = field_class(**data) - return type(serializer_name, (serializers.Serializer,), serializer_fields)