模型类序列化器
7.1 序列化多条数据
序列化器
class StudentModelSerializer(serializers.ModelSerializer):
"""学生信息序列化器"""
class Meta:
model = studentsInfo # 必填
fields = ["id", "name", "sex", "age", "classroom", "info"] # 必填
read_only_fields = ["id"] # 选填
exclude = [] # 选填,列表里的字段不会序列化
视图
class StudentView(View):
def get(self, request):
"""序列化器-模型序列化器的调用"""
# 1.获取数据集
queryset = studentsInfo.objects.all()
# 2.实例化序列化器,得到序列化器对象
serializer = StudentModelSerializer(instance=queryset, many=True)
# 3.调用序列化器对象的data属性获取序列i化后的数据
data = serializer.data
# 4.响应数据
return JsonResponse(data=data, status=200, safe=False, json_dumps_params={"ensure_ascii": False})
响应结果
[
{
"id": 1,
"name": "小明",
"sex": "男",
"age": 22,
"classroom": "301",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 2,
"name": "小红",
"sex": "女",
"age": 18,
"classroom": "301",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 4,
"name": "李四",
"sex": "男",
"age": 24,
"classroom": "308",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 5,
"name": "闵麒良",
"sex": "男",
"age": 23,
"classroom": "310",
"info": "河工胡歌"
},
{
"id": 12,
"name": "孙明辉",
"sex": "男",
"age": 23,
"classroom": "333",
"info": "这个人很懒,什么都没有留下"
},
{
"id": 15,
"name": "彭于晏",
"sex": "女",
"age": 0,
"classroom": "000",
"info": "要做个猛男,要猛!"
}
]
7.2 添加额外字段
序列化器
class StudentModelSerializer(serializers.ModelSerializer):
"""学生信息序列化器"""
nickname = serializers.CharField(read_only=True)
class Meta:
model = studentsInfo # 必填
fields = ["id", "name", "sex", "age", "classroom", "info","nickname"] # 必填
read_only_fields = ["id"] # 选填
视图
class StudentView(View):
def get(self, request):
"""序列化器-模型序列化器的调用"""
# 1.获取数据集
queryset = studentsInfo.objects.all()
for data in queryset:
data.nickname = "胡歌"
# 2.实例化序列化器,得到序列化器对象
serializer = StudentModelSerializer(instance=queryset, many=True)
# 3.调用序列化器对象的data属性获取序列i化后的数据
data = serializer.data
# 4.响应数据
return JsonResponse(data=data, status=200, safe=False, json_dumps_params={"ensure_ascii": False})
响应结果
[
{
"id": 1,
"name": "小明",
"sex": "男",
"age": 22,
"classroom": "301",
"info": "这个人很懒,什么也没有留下",
"nickname": "胡歌"
},
{
"id": 2,
"name": "小红",
"sex": "女",
"age": 18,
"classroom": "301",
"info": "这个人很懒,什么也没有留下",
"nickname": "胡歌"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下",
"nickname": "胡歌"
},
{
"id": 4,
"name": "李四",
"sex": "男",
"age": 24,
"classroom": "308",
"info": "这个人很懒,什么也没有留下",
"nickname": "胡歌"
},
{
"id": 5,
"name": "闵麒良",
"sex": "男",
"age": 23,
"classroom": "310",
"info": "河工胡歌",
"nickname": "胡歌"
},
{
"id": 12,
"name": "孙明辉",
"sex": "男",
"age": 23,
"classroom": "333",
"info": "这个人很懒,什么都没有留下",
"nickname": "胡歌"
},
{
"id": 15,
"name": "彭于晏",
"sex": "女",
"age": 0,
"classroom": "000",
"info": "要做个猛男,要猛!",
"nickname": "胡歌"
}
]
7.3 字段选项验证
序列化器
class StudentModelSerializer(serializers.ModelSerializer):
"""学生信息序列化器"""
nickname = serializers.CharField(read_only=True)
class Meta:
model = studentsInfo # 必填
fields = ["id", "name", "sex", "age", "classroom", "info","nickname"] # 必填
read_only_fields = ["id"] # 选填
extra_kwargs = {
"age": {
"min_value": 0,
"max_value": 100,
"error_messages": {
"min_value": "the age must be age>=0",
"max_value": "the age must be age<=100"
}
}
} # 选填
视图
class StudentView(View):
def get(self, request):
"""反序列化-采用字段选项来验证数据-验证失败不抛出异常"""
# 1.接收客户端提交的数据
# data = json.loads(request.body)
data = {
"name": "张家祥",
"age": 120,
"sex": "男",
"classroom": "444",
"info": "这个人很胖"
}
# 2.实例化序列化器,获取序列化对象
serializer = StudentModelSerializer(data=data)
# 3.调用序列化器进行数据验证
ret = serializer.is_valid()
# 4.获取验证以后的结果
if ret:
return JsonResponse(dict(serializer.validated_data), safe=False)
else:
return JsonResponse(dict(serializer.errors), safe=False)
# 5.操作数据库
# 6.返回结果
响应结果
{
"age": [
"the age must be age<=100"
]
}
7.4 反序列化(添加数据)
视图
class StudentView(View):
def get(self, request):
"""反序列化-采用字段选项来验证数据-验证失败抛出异常"""
# 1.接收客户端提交的数据
# data = json.loads(request.body)
data = {
"name": "彭于晏",
"age": 30,
"sex": "男",
"classroom": "666",
"info": "要做个猛男,要猛!"
}
# 2.实例化序列化器,获取序列化对象
serializer = StudentModelSerializer(data=data)
# 3.调用序列化器进行数据验证
serializer.is_valid(raise_exception=True) # 抛出异常,代码不会往下执
# 4.获取验证以后的结果,操作数据库
serializer.save() # 如果在实例化序列化器对象的时候没有传入instance属性,则自动调用序列化器的create钩子函数方法
# 5.返回结果
return JsonResponse(serializer.data, status=201)
响应结果
{
"id": 16,
"name": "范玉才",
"sex": "男",
"age": 23,
"classroom": "310",
"info": "要做个猛男,要猛!"
}
数据库变化
注意:模型类序列化器内部已经实现了create和update方法,无需我们实现,如果要是有特殊需求可以重写,但是验证函数要自己定义,和普通序列化器的使用方法相同,修改数据的话和之前的视图一样,这里不再一一实现。
标签:name,模型,sex,id,序列化,data,age From: https://www.cnblogs.com/minqiliang/p/16822140.html