Merge pull request #10729 from jumpserver/pr@dev@perf_custom_asset_support_required_field

perf: 自定义 field 支持 required
pull/10731/head
老广 2023-06-15 13:18:06 +08:00 committed by GitHub
commit 54926f7c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -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)