序列化校验方法里面的参数是如何变化

# 校验字段
ser = self.get_serializer(data=request.data) "这里的requesst.data是一个字典"
ser.is_valid(raise_exception=True)  "执行BaseSerilazer里面的is_valid"

""
BaseSerializer
""
 def is_valid(self, *, raise_exception=False):
        assert hasattr(self, 'initial_data'), (
            'Cannot call `.is_valid()` as no `data=` keyword argument was '
            'passed when instantiating the serializer instance.'
        )

        if not hasattr(self, '_validated_data'):
            try:
                self._validated_data = self.run_validation(self.initial_data) "执行子类Serializer的run_validation,经过这个方法后返回的数据变成了有序字典, "
            except ValidationError as exc:
                self._validated_data = {}
                self._errors = exc.detail
            else:
                self._errors = {}

        if self._errors and raise_exception:
            raise ValidationError(self.errors)

        return not bool(self._errors)

""
Serializer
""
    def run_validation(self, data=empty):
        (is_empty_value, data) = self.validate_empty_values(data)
        if is_empty_value:
            return data

        value = self.to_internal_value(data) "这里的方法将字典变成有序字典"
        try:
            self.run_validators(value)
            value = self.validate(value) "这里执行自定义validate的校验"
            assert value is not None, '.validate() should return the validated data'
        except (ValidationError, DjangoValidationError) as exc:
            raise ValidationError(detail=as_serializer_error(exc))

        return value

""
BaseSeiralizer
""
def save(self,validate_data)
        validated_data = {**self.validated_data, **kwargs} "这个地方又把有序字典转成字典,在未调用save方法之前,validate_data一直是个有序字典"

        if self.instance is not None:
            self.instance = self.update(self.instance, validated_data)
            assert self.instance is not None, (
                '`update()` did not return an object instance.'
            )
        else:
            self.instance = self.create(validated_data)
            assert self.instance is not None, (
                '`create()` did not return an object instance.'
            )

        return self.instance

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
序列和反序列是指将数据结构转换为字符串,或者将字符串转换为数据结构的过程。在Web开发中,序列和反序列URL参数可以让我们更方便地传递复杂数据结构。 序列URL参数:将数据结构(如JSON对象)转换为URL参数字符串的过程。通常使用encodeURIComponent()函数来编码字符串。 反序列URL参数:将URL参数字符串转换为数据结构的过程。通常使用decodeURIComponent()函数来解码字符串,并使用JSON.parse()函数将字符串转换为JSON对象。 例如,假设我们要传递以下JSON对象作为URL参数: { "name": "张三", "age": 18, "hobbies": ["reading", "music", "coding"] } 我们可以通过序列和反序列实现: 序列URL参数: var data = { "name": "张三", "age": 18, "hobbies": ["reading", "music", "coding"] }; var queryString = Object.keys(data).map(function(key) { return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]); }).join('&'); console.log(queryString); // 输出:name=%E5%BC%A0%E4%B8%89&age=18&hobbies=reading&hobbies=music&hobbies=coding 反序列URL参数: var queryString = 'name=%E5%BC%A0%E4%B8%89&age=18&hobbies=reading&hobbies=music&hobbies=coding'; var data = {}; queryString.split('&').forEach(function(keyValuePair) { var parts = keyValuePair.split('='); var key = decodeURIComponent(parts); var value = decodeURIComponent(parts); if (!data[key]) { data[key] = []; } data[key].push(value); }); console.log(JSON.stringify(data)); // 输出:{"name":["张三"],"age":["18"],"hobbies":["reading","music","coding"]}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值