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
上一篇: 想买个宠物