|
|
|
@ -44,19 +44,21 @@ def set_default_by_type(tp, data, field_info):
|
|
|
|
|
|
|
|
|
|
def create_serializer_class(serializer_name, fields_info):
|
|
|
|
|
serializer_fields = {}
|
|
|
|
|
fields_name = ['name', 'label', 'default', 'type', 'help_text']
|
|
|
|
|
fields_name = ['name', 'label', 'default', 'required', 'type', 'help_text']
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
# 用户定义 default 和 required 可能会冲突, 所以要处理一下
|
|
|
|
|
default = data.get('default', '')
|
|
|
|
|
if default not in ['', None]:
|
|
|
|
|
data['required'] = False
|
|
|
|
|
else:
|
|
|
|
|
data.pop('default', None)
|
|
|
|
|
data['required'] = field_info.get('required', True)
|
|
|
|
|
data['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)
|
|
|
|
|