def -- 反序列化字段校验通过的新增数据和修改数据
作者:互联网
一.新增数据的create方法
1.serializers.py代码示例
from rest_framework import serializers
from student import models
class StudentSerializer(serializers.Serializer):
name = serializers.CharField(max_length=8,required=True)
age = serializers.IntegerField(max_value=120,required=True)
sex = serializers.IntegerField(default=1)
def validate_name(self,attr):
if attr == "老王":
raise serializers.ValidationError('隔壁老王不能进')
return attr
def validate(self, attrs):
if attrs.get('name') == "张三" and attrs.get('age') == 26:
raise serializers.ValidationError('26岁的张三不能进')
return attrs
def create(self, validated_data):
stu_data = models.Student.objects.create(**validated_data)
return stu_data
2.views.py代码示例:
from django.views import View
from django.http import HttpResponse
from unsers.serializers import StudentSerializer
from student import models
import json
# Create your views here.
class StudentView(View):
def post(self,request):
data = json.loads(request.body)
#将序列化的数据传入定义的反序列化类中,用data参数接收
serializers = StudentSerializer(data=data)
serializers.is_valid(raise_exception=True) #raise_exception=True该参数可以在校验失败时,直接报错
serializers.save() #在校验通过时,会直接执行StudentSerializer类中的create方法
return HttpResponse('ok')
2.修改数据的update方法:
serializers.py代码示例:
from rest_framework import serializers
from student import models
class StudentSerializer(serializers.Serializer):
name = serializers.CharField(max_length=8,required=True)
age = serializers.IntegerField(max_value=120,required=True)
sex = serializers.IntegerField(default=1)
def validate_name(self,attr):
if attr == "老王":
raise serializers.ValidationError('隔壁老王不能进')
return attr
def validate(self, attrs):
if attrs.get('name') == "张三" and attrs.get('age') == 26:
raise serializers.ValidationError('26岁的张三不能进')
return attrs
def create(self, validated_data):
stu_data = models.Student.objects.create(**validated_data)
return stu_data
def update(self, instance, validated_data):
#利用反射,将获取的数据,进行更新
for key, value in validated_data.items():
setattr(instance, key, value)
#或者每个字段单独的更新
# instance.name = validated_data.get('name')
# instance.age = validated_data.get('age')
# instance.sex = validated_data.get('sex')
instance.save()
#保存后,将对象返回
return instance
views.py代码示例:
from django.views import View
from django.http import HttpResponse
from unsers.serializers import StudentSerializer
from student import models
import json
# Create your views here.
class StudentView(View):
def post(self,request):
data = json.loads(request.body)
#将序列化的数据传入定义的反序列化类中,用data参数接收
serializers = StudentSerializer(data=data)
serializers.is_valid(raise_exception=True) #raise_exception=True该参数可以在校验失败时,直接报错
serializers.save() #在校验通过时,会直接执行StudentSerializer类中的create方法
return HttpResponse('ok')
def put(self,request):
data = json.loads(request.body)
id = data.get('id')
stu_obj = models.Student.objects.get(id=id)
serializers=StudentSerializer(instance=stu_obj,data=data)
serializers.is_valid(raise_exception=True)
serializers.save() #在校验通过时,会直接执行StudentSerializer类中的update方法
return HttpResponse('ok')
标签:serializers,--,StudentSerializer,return,import,序列化,data,def 来源: https://www.cnblogs.com/zhiqianggege/p/16218877.html