2023-04-18 09:07:01 +00:00
|
|
|
|
import logging
|
|
|
|
|
|
2023-02-02 12:10:48 +00:00
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from drf_writable_nested.serializers import WritableNestedModelSerializer as NestedModelSerializer
|
2021-01-01 23:25:23 +00:00
|
|
|
|
from rest_framework import serializers
|
2020-07-29 06:57:44 +00:00
|
|
|
|
from rest_framework.serializers import ModelSerializer
|
2023-02-02 12:10:48 +00:00
|
|
|
|
from rest_framework.serializers import Serializer
|
2020-07-29 06:57:44 +00:00
|
|
|
|
from rest_framework_bulk.serializers import BulkListSerializer
|
2022-04-28 14:54:18 +00:00
|
|
|
|
|
2022-09-26 02:43:18 +00:00
|
|
|
|
from .mixin import BulkListSerializerMixin, BulkSerializerMixin
|
|
|
|
|
|
2021-01-01 23:25:23 +00:00
|
|
|
|
__all__ = [
|
2022-08-08 06:34:57 +00:00
|
|
|
|
'MethodSerializer', 'EmptySerializer', 'BulkModelSerializer',
|
2022-10-24 12:14:18 +00:00
|
|
|
|
'AdaptedBulkListSerializer', 'CeleryTaskExecutionSerializer',
|
2022-11-03 08:55:38 +00:00
|
|
|
|
'WritableNestedModelSerializer', 'GroupedChoiceSerializer',
|
2023-05-31 08:21:24 +00:00
|
|
|
|
'FileSerializer', 'DictSerializer'
|
2021-01-01 23:25:23 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
|
class MethodSerializer(serializers.Serializer):
|
|
|
|
|
def __init__(self, method_name=None, **kwargs):
|
|
|
|
|
self.method_name = method_name
|
2021-01-05 15:39:38 +00:00
|
|
|
|
super().__init__(**kwargs)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-11 07:31:28 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
# 生成swagger时使用
|
|
|
|
|
ref_name = None
|
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
def bind(self, field_name, parent):
|
2021-01-06 04:44:12 +00:00
|
|
|
|
if self.method_name is None:
|
|
|
|
|
method_name = 'get_{field_name}_serializer'.format(field_name=field_name)
|
|
|
|
|
self.method_name = method_name
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
super().bind(field_name, parent)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
@cached_property
|
2021-01-06 04:44:12 +00:00
|
|
|
|
def serializer(self) -> serializers.Serializer:
|
|
|
|
|
method = getattr(self.parent, self.method_name)
|
2023-04-18 09:07:01 +00:00
|
|
|
|
try:
|
|
|
|
|
_serializer = method()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(e, exc_info=True)
|
|
|
|
|
raise e
|
2021-01-12 10:06:42 +00:00
|
|
|
|
# 设置serializer的parent值,否则在serializer实例中获取parent会出现断层
|
|
|
|
|
setattr(_serializer, 'parent', self.parent)
|
|
|
|
|
return _serializer
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
@cached_property
|
|
|
|
|
def fields(self):
|
|
|
|
|
"""
|
2021-01-06 04:44:12 +00:00
|
|
|
|
重写此方法因为在 BindingDict 中要设置每一个 field 的 parent 为 `serializer`,
|
2021-01-05 15:39:38 +00:00
|
|
|
|
这样在调用 field.parent 时, 才会达到预期的结果,
|
|
|
|
|
比如: serializers.SerializerMethodField
|
|
|
|
|
"""
|
2021-01-12 10:06:42 +00:00
|
|
|
|
return self.serializer.fields
|
|
|
|
|
|
|
|
|
|
def run_validation(self, data=serializers.empty):
|
|
|
|
|
return self.serializer.run_validation(data)
|
|
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
return self.serializer.to_representation(instance)
|
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
return self.serializer.get_initial()
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
2020-07-06 03:14:20 +00:00
|
|
|
|
class EmptySerializer(Serializer):
|
|
|
|
|
pass
|
2020-07-29 06:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulkModelSerializer(BulkSerializerMixin, ModelSerializer):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AdaptedBulkListSerializer(BulkListSerializerMixin, BulkListSerializer):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-10-24 12:14:18 +00:00
|
|
|
|
class CeleryTaskExecutionSerializer(serializers.Serializer):
|
2020-07-29 06:57:44 +00:00
|
|
|
|
task = serializers.CharField(read_only=True)
|
2021-01-01 23:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
2022-04-28 14:54:18 +00:00
|
|
|
|
class ChoiceSerializer(serializers.Serializer):
|
2022-09-20 08:18:23 +00:00
|
|
|
|
label = serializers.CharField(label=_("Label"))
|
2022-04-28 14:54:18 +00:00
|
|
|
|
value = serializers.CharField(label=_("Value"))
|
|
|
|
|
|
|
|
|
|
|
2022-09-13 02:41:49 +00:00
|
|
|
|
class GroupedChoiceSerializer(ChoiceSerializer):
|
2022-04-28 14:54:18 +00:00
|
|
|
|
children = ChoiceSerializer(many=True, label=_("Children"))
|
|
|
|
|
|
2022-07-17 06:28:55 +00:00
|
|
|
|
|
2022-10-19 02:21:05 +00:00
|
|
|
|
class WritableNestedModelSerializer(NestedModelSerializer):
|
2022-08-09 08:53:43 +00:00
|
|
|
|
pass
|
2022-11-03 08:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileSerializer(serializers.Serializer):
|
|
|
|
|
file = serializers.FileField(label=_("File"))
|
2023-04-20 07:23:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DictSerializer(serializers.Serializer):
|
|
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
# 返回一个包含所有提交字段的 Python 字典
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
|
# 确保从请求中得到的输入是 Python 字典
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
return data
|
|
|
|
|
else:
|
|
|
|
|
raise serializers.ValidationError("无法转换为dict类型")
|