欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

django序列化数据

程序员文章站 2022-05-19 10:25:53
...

序列化时间:


class MsgSerializer(serializers.ModelSerializer):
    time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')

    class Meta:
        model = models.Msg
        fields = ['id','content','time','status']
        depth = 0

数据校验:

class CheckSmsSerializer(serializers.Serializer):
    phone = serializers.CharField(max_length=11,min_length=11)
    sms_code = serializers.CharField(max_length=6,min_length=6)
    def validate(self,data):
        redis = get_redis_connection('default')
        sms=redis.get(data['phone'])
        if data['sms_code'] != sms.decode('utf-8'):
            raise serializers.ValidationError({'info':'手機驗證碼錯誤!','code':400})
        return data

参考:https://blog.csdn.net/l_vip/article/details/79156113

相关标签: django python