一 序列化器-Serializer
作用:
1 2 3 1. 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串2. 反序列化,把客户端发送过来的数据,经过request以后变成字典,序列化器可以把字典转成模型3. 反序列化,完成数据校验功能
1.1 定义序列化器 Django REST framework中的Serializer使用类来定义,须继承自rest_framework.serializers.Serializer。
接下来,为了方便演示序列化器的使用,我们先创建一个新的子应用sers
1 python manage.py startapp sers
我们已有了一个数据库模型类students/Student
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from django.db import modelsclass Student (models.Model): name = models.CharField(max_length=100 ,verbose_name="姓名" ,help_text="提示文本:账号不能为空!" ) sex = models.BooleanField(default=True ,verbose_name="性别" ) age = models.IntegerField(verbose_name="年龄" ) class_null = models.CharField(max_length=5 ,verbose_name="班级编号" ) description = models.TextField(verbose_name="个性签名" ) class Meta : db_table="tb_student" verbose_name = "学生" verbose_name_plural = verbose_name
我们想为这个模型类提供一个序列化器,可以定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 from rest_framework import serializersclass StudentSerializer (serializers.Serializer): """学生信息序列化器""" id = serializers.IntegerField() name = serializers.CharField() age = serializers.IntegerField() sex = serializers.BooleanField() description = serializers.CharField()
注意:serializer不是只能为数据库模型类定义,也可以为非数据库模型类的数据定义。 serializer是独立于数据库之外的存在。
1.1.1 常用字段类型
字段
字段构造方式
BooleanField
BooleanField()
NullBooleanField
NullBooleanField()
CharField
CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)
EmailField
EmailField(max_length=None, min_length=None, allow_blank=False)
RegexField
RegexField(regex, max_length=None, min_length=None, allow_blank=False)
SlugField
SlugField(maxlength=50, min_length=None, allow_blank=False) 正则字段,验证正则模式 [a-zA-Z0-9 -]+
URLField
URLField(max_length=200, min_length=None, allow_blank=False)
UUIDField
UUIDField(format=’hex_verbose’) format: 1) 'hex_verbose'
如"5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
2) 'hex'
如 "5ce0e9a55ffa654bcee01238041fb31a"
3)'int'
- 如: "123456789012312313134124512351145145114"
4)'urn'
如: "urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
IPAddressField
IPAddressField(protocol=’both’, unpack_ipv4=False, **options)
IntegerField
IntegerField(max_value=None, min_value=None)
FloatField
FloatField(max_value=None, min_value=None)
DecimalField
DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None) max_digits: 最多位数 decimal_palces: 小数点位置
DateTimeField
DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None)
DateField
DateField(format=api_settings.DATE_FORMAT, input_formats=None)
TimeField
TimeField(format=api_settings.TIME_FORMAT, input_formats=None)
DurationField
DurationField()
ChoiceField
ChoiceField(choices) choices与Django的用法相同
MultipleChoiceField
MultipleChoiceField(choices)
FileField
FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ImageField
ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ListField
ListField(child=, min_length=None, max_length=None)
DictField
DictField(child=)
选项参数:
参数名称
作用
max_length
最大长度
min_lenght
最小长度
allow_blank
是否允许为空
trim_whitespace
是否截断空白字符
max_value
最小值
min_value
最大值
通用参数:
参数名称
说明
read_only
表明该字段仅用于序列化输出,默认False
write_only
表明该字段仅用于反序列化输入,默认False
required
表明该字段在反序列化时必须输入,默认True
default
反序列化时使用的默认值
allow_null
表明该字段是否允许传入None,默认False
validators
该字段使用的验证器
error_messages
包含错误编号与错误信息的字典
label
用于HTML展示API页面时,显示的字段名称
help_text
用于HTML展示API页面时,显示的字段帮助提示信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 from django.db import modelssex_choice = ( (0 , '女性' ), (1 , '男性' ), ) class Student (models.Model): name=models.CharField(max_length=32 ) sex=models.IntegerField(choices=sex_choice,default=1 ) age=models.IntegerField() class MyChoiceField (serializers.ChoiceField): def to_representation (self, obj ): """返回状态名称""" if obj == '' and self.allow_blank: return obj return self._choices[obj] def to_internal_value (self, data ): """支持choice的key 或value名称的写入""" for i in self._choices: if i == data or self._choices[i] == data: return i raise serializers.ValidationError("Acceptable values are {0}." .format (list (self._choices.values()))) class StudentSerializer (serializers.Serializer): name = serializers.CharField() sex = MyChoiceField(choices=((1 ,'男性' ),(0 ,'女性' )),required=False ) age = serializers.IntegerField() def update (self, instance, validated_data ): instance.name=validated_data.get('name' ) instance.sex=validated_data.get('sex' ) instance.age=validated_data.get('age' ) instance.save() return instance def create (self, validated_data ): instance=Student.objects.create(**validated_data) return instance
1.2 创建Serializer对象 定义好Serializer类后,就可以创建Serializer对象了。
Serializer的构造方法为:
1 Serializer(instance=None , data=empty, **kwarg)
说明:
1)用于序列化时,将模型类对象传入instance 参数
2)用于反序列化时,将要被反序列化的数据传入data 参数
3)除了instance和data参数外,在构造Serializer对象时,还可通过context 参数额外添加数据,如
1 serializer = AccountSerializer(account, context={'request' : request})
通过context参数附加的数据,可以通过Serializer对象的context属性获取。
使用序列化器的时候一定要注意,序列化器声明了以后,不会自动执行,需要我们在视图中进行调用才可以。
序列化器无法直接接收数据,需要我们在视图中创建序列化器对象时把使用的数据传递过来。
序列化器的字段声明类似于我们前面使用过的表单系统。
开发restful api时,序列化器会帮我们把模型数据转换成字典.
drf提供的视图会帮我们把字典转换成json,或者把客户端发送过来的数据转换字典.
1.3 序列化器的使用 序列化器的使用分两个阶段:
在客户端请求时,使用序列化器可以完成对数据的反序列化。
在服务器响应时,使用序列化器可以完成对数据的序列化。
1.3.1 序列化 1.3.1.1 基本使用 1) 先查询出一个学生对象
1 2 3 from students.models import Studentstudent = Student.objects.get(id =3 )
2) 构造序列化器对象
1 2 3 from .serializers import StudentSerializerserializer = StudentSerializer(instance=student)
3)获取序列化数据
通过data属性可以获取序列化后的数据
1 2 serializer.data # {'id' : 4 , 'name' : '小张' , 'age' : 18 , 'sex' : True , 'description' : '猴赛雷' }
完整视图代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 from django.views import Viewfrom students.models import Studentfrom .serializers import StudentSerializerfrom django.http.response import JsonResponseclass StudentView (View ): """使用序列化器序列化转换单个模型数据""" def get (self,request,pk ): student = Student.objects.get(pk=pk) serializer = StudentSerializer(instance=student) print (serializer.data) return JsonResponse(serializer.data)
4)如果要被序列化的是包含多条数据的查询集QuerySet,可以通过添加many=True 参数补充说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 """使用序列化器序列化转换多个模型数据""" def get (self,request ): student_list = Student.objects.all () serializer = StudentSerializer(instance=student_list,many=True ) print ( serializer.data ) return JsonResponse(serializer.data,safe=False )
1.3.1.2 高级用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 from django.db import modelsclass Book (models.Model): title=models.CharField(max_length=32 ) price=models.IntegerField() pub_date=models.DateField() publish=models.ForeignKey("Publish" ,on_delete=models.CASCADE,null=True ) authors=models.ManyToManyField("Author" ) def __str__ (self ): return self.title class Publish (models.Model): name=models.CharField(max_length=32 ) email=models.EmailField() def __str__ (self ): return self.name class Author (models.Model): name=models.CharField(max_length=32 ) age=models.IntegerField() def __str__ (self ): return self.name from rest_framework import serializersfrom app01.models import Bookclass BookSerializers (serializers.Serializer): id =serializers.CharField(read_only=True ) title=serializers.CharField(max_length=32 ) price=serializers.IntegerField() pub_date=serializers.DateField() publish=serializers.CharField(source="publish.name" ,default='xxx' ) authors=serializers.SerializerMethodField(read_only=True ) def get_authors (self,obj ): temp=[] for author in obj.authors.all (): temp.append(author.name) return temp def create (self, validated_data ): print (validated_data) publish_id=validated_data.get('publish' ).get('name' ) print (publish_id) del validated_data['publish' ] return Book.objects.create(publish_id=publish_id,**validated_data) def update (self, instance, validated_data ): print (validated_data.get('aa' )) instance.title = validated_data.get('title' , instance.title) instance.price = validated_data.get('price' , instance.price) instance.pub_date = validated_data.get('pub_date' , instance.pub_date) print (validated_data.get('publish' , instance.publish)) instance.publish_id = validated_data.get('publish' , instance.publish).get('name' ) instance.save() return instance from django.shortcuts import render,HttpResponsefrom app01 import modelsfrom django.http import HttpRequestfrom rest_framework.views import APIViewfrom app01.models import Bookfrom rest_framework.response import Responsefrom app01.ser import BookSerializersclass BookViewSet (APIView ): def get (self,request,*args,**kwargs ): book_list=Book.objects.all () bs=BookSerializers(book_list,many=True ) return Response(bs.data) def post (self,request,*args,**kwargs ): bs=BookSerializers(data=request.data) bs.is_valid(raise_exception=True ) bs.save() return Response(bs.data) class BookDetailView (APIView ): def get (self,request,pk ): book_obj=models.Book.objects.filter (pk=pk).first() bs=BookSerializers(book_obj,many=False ) return Response(bs.data) def put (self,request,pk ): book_obj = models.Book.objects.filter (pk=pk).first() bs=BookSerializers(instance=book_obj,data=request.data,partial=True ) if bs.is_valid(): bs.save(aa="lqz" ) return Response(bs.data) else : return Response(bs.errors) def delete (self,request,pk ): models.Book.objects.filter (pk=pk).delete() return Response("" ) from django.contrib import adminfrom django.urls import path,re_pathfrom app01 import viewsurlpatterns = [ path('admin/' , admin.site.urls), path('books/' , views.BookViewSet.as_view()), re_path('books/(?P<pk>\d+)/' , views.BookDetailView.as_view()), ]
注意:
source 如果是字段,会显示字段,如果是方法,会执行方法,不用加括号(authors=serializers.CharField(source=’authors.all’))
如在模型中定义一个方法,直接可以在在source指定执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class UserInfo (models.Model): user_type_choices = ( (1 ,'普通用户' ), (2 ,'VIP' ), (3 ,'SVIP' ), ) user_type = models.IntegerField(choices=user_type_choices) username = models.CharField(max_length=32 ,unique=True ) password = models.CharField(max_length=64 ) ret=models.UserInfo.objects.filter (pk=1 ).first() aa=ret.get_user_type_display() xx=serializers.CharField(source='get_user_type_display' )
1.3.2 反序列化 1.3.2.1 数据验证 使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象。
在获取反序列化的数据前,必须调用**is_valid()**方法进行验证,验证成功返回True,否则返回False。
验证失败,可以通过序列化器对象的errors 属性获取错误信息,返回字典,包含了字段和字段的错误。如果是非字段错误,可以通过修改REST framework配置中的NON_FIELD_ERRORS_KEY 来控制错误字典中的键名。
验证成功,可以通过序列化器对象的validated_data 属性获取数据。
在定义序列化器时,指明每个字段的序列化类型和选项参数,本身就是一种验证行为。
如我们前面定义过的BookInfoSerializer
1 2 3 4 5 6 7 8 class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" id = serializers.IntegerField(label='ID' , read_only=True ) btitle = serializers.CharField(label='名称' , max_length=20 ) bpub_date = serializers.DateField(label='发布日期' , required=False ) bread = serializers.IntegerField(label='阅读量' , required=False ) bcomment = serializers.IntegerField(label='评论量' , required=False ) image = serializers.ImageField(label='图片' , required=False )
通过构造序列化器对象,并将要反序列化的数据传递给data构造参数,进而进行验证
1 2 3 4 5 6 7 8 9 10 11 12 13 from booktest.serializers import BookInfoSerializerdata = {'bpub_date' : 123 } serializer = BookInfoSerializer(data=data) serializer.is_valid() serializer.errors serializer.validated_data data = {'btitle' : 'python' } serializer = BookInfoSerializer(data=data) serializer.is_valid() serializer.errors serializer.validated_data
is_valid()方法还可以在验证失败时抛出异常serializers.ValidationError,可以通过传递raise_exception=True 参数开启,REST framework接收到此异常,会向前端返回HTTP 400 Bad Request响应。
1 2 serializer.is_valid(raise_exception=True )
如果觉得这些还不够,需要再补充定义验证行为,可以使用以下三种方法:
1) validate_字段名 对<field_name>
字段进行验证,如
1 2 3 4 5 6 7 8 class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" ... def validate_btitle (self, value ): if 'django' not in value.lower(): raise serializers.ValidationError("图书不是关于Django的" ) return value
测试
1 2 3 4 5 6 from booktest.serializers import BookInfoSerializerdata = {'btitle' : 'python' } serializer = BookInfoSerializer(data=data) serializer.is_valid() serializer.errors
2) validate 在序列化器中需要同时对多个字段进行比较验证时,可以定义validate方法来验证,如
1 2 3 4 5 6 7 8 9 10 class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" ... def validate (self, attrs ): bread = attrs['bread' ] bcomment = attrs['bcomment' ] if bread < bcomment: raise serializers.ValidationError('阅读量小于评论量' ) return attrs
测试
1 2 3 4 5 6 from booktest.serializers import BookInfoSerializerdata = {'btitle' : 'about django' , 'bread' : 10 , 'bcomment' : 20 } s = BookInfoSerializer(data=data) s.is_valid() s.errors
3) validators 在字段中添加validators选项参数,也可以补充验证行为,如
1 2 3 4 5 6 7 8 9 10 11 12 def about_django (value ): if 'django' not in value.lower(): raise serializers.ValidationError("图书不是关于Django的" ) class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" id = serializers.IntegerField(label='ID' , read_only=True ) btitle = serializers.CharField(label='名称' , max_length=20 , validators=[about_django]) bpub_date = serializers.DateField(label='发布日期' , required=False ) bread = serializers.IntegerField(label='阅读量' , required=False ) bcomment = serializers.IntegerField(label='评论量' , required=False ) image = serializers.ImageField(label='图片' , required=False )
测试:
1 2 3 4 5 6 from booktest.serializers import BookInfoSerializerdata = {'btitle' : 'python' } serializer = BookInfoSerializer(data=data) serializer.is_valid() serializer.errors
1.3.2.2 反序列化-保存数据 前面的验证数据成功后,我们可以使用序列化器来完成数据反序列化的过程.这个过程可以把数据转成模型类对象.
可以通过实现create()和update()两个方法来实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" ... def create (self, validated_data ): """新建""" return BookInfo(**validated_data) def update (self, instance, validated_data ): """更新,instance为要更新的对象实例""" instance.btitle = validated_data.get('btitle' , instance.btitle) instance.bpub_date = validated_data.get('bpub_date' , instance.bpub_date) instance.bread = validated_data.get('bread' , instance.bread) instance.bcomment = validated_data.get('bcomment' , instance.bcomment) return instance
如果需要在返回数据对象的时候,也将数据保存到数据库中,则可以进行如下修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class BookInfoSerializer (serializers.Serializer): """图书数据序列化器""" ... def create (self, validated_data ): """新建""" return BookInfo.objects.create(**validated_data) def update (self, instance, validated_data ): """更新,instance为要更新的对象实例""" instance.btitle = validated_data.get('btitle' , instance.btitle) instance.bpub_date = validated_data.get('bpub_date' , instance.bpub_date) instance.bread = validated_data.get('bread' , instance.bread) instance.bcomment = validated_data.get('bcomment' , instance.bcomment) instance.save() return instance
实现了上述两个方法后,在反序列化数据的时候,就可以通过save()方法返回一个数据对象实例了
1 book = serializer.save()
如果创建序列化器对象的时候,没有传递instance实例,则调用save()方法的时候,create()被调用,相反,如果传递了instance实例,则调用save()方法的时候,update()被调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 from db.serializers import BookInfoSerializerdata = {'btitle' : '封神演义' } serializer = BookInfoSerializer(data=data) serializer.is_valid() serializer.save() from db.models import BookInfobook = BookInfo.objects.get(id =2 ) data = {'btitle' : '倚天剑' } serializer = BookInfoSerializer(book, data=data) serializer.is_valid() serializer.save() book.btitle
1.3.2.3 附加说明 1) 在对序列化器进行save()保存时,可以额外传递数据,这些数据可以在create()和update()中的validated_data参数获取到
1 2 serializer.save(owner=request.user)
2)默认序列化器必须传递所有required的字段,否则会抛出验证异常。但是我们可以使用partial参数来允许部分字段更新
1 2 serializer = CommentSerializer(comment, data={'content' : u'foo bar' }, partial=True )
1.3.3 模型类序列化器 如果我们想要使用序列化器对应的是Django的模型类,DRF为我们提供了ModelSerializer模型类序列化器来帮助我们快速创建一个Serializer类。
ModelSerializer与常规的Serializer相同,但提供了:
基于模型类自动生成一系列字段
基于模型类自动为Serializer生成validators,比如unique_together
包含默认的create()和update()的实现
1.3.3.1 定义 比如我们创建一个BookInfoSerializer
1 2 3 4 5 class BookInfoSerializer (serializers.ModelSerializer): """图书数据序列化器""" class Meta : model = BookInfo fields = '__all__'
model 指明参照哪个模型类
fields 指明为模型类的哪些字段生成
我们可以在python manage.py shell中查看自动生成的BookInfoSerializer的具体实现
1 2 3 4 5 6 7 8 9 10 >>> from booktest.serializers import BookInfoSerializer>>> serializer = BookInfoSerializer()>>> serializerBookInfoSerializer(): id = IntegerField(label='ID' , read_only=True ) btitle = CharField(label='名称' , max_length=20 ) bpub_date = DateField(allow_null=True , label='发布日期' , required=False ) bread = IntegerField(label='阅读量' , max_value=2147483647 , min_value=-2147483648 , required=False ) bcomment = IntegerField(label='评论量' , max_value=2147483647 , min_value=-2147483648 , required=False ) image = ImageField(allow_null=True , label='图片' , max_length=100 , required=False )
1.3.3.2 指定字段
使用fields 来明确字段,__all__
表名包含所有字段,也可以写明具体哪些字段,如
1 2 3 4 5 class BookInfoSerializer (serializers.ModelSerializer): """图书数据序列化器""" class Meta : model = BookInfo fields = ('id' , 'btitle' , 'bpub_date' )
使用exclude 可以明确排除掉哪些字段
1 2 3 4 5 class BookInfoSerializer (serializers.ModelSerializer): """图书数据序列化器""" class Meta : model = BookInfo exclude = ('image' ,)
显示指明字段,如:
1 2 3 4 5 6 class HeroInfoSerializer (serializers.ModelSerializer): hbook = BookInfoSerializer() class Meta : model = HeroInfo fields = ('id' , 'hname' , 'hgender' , 'hcomment' , 'hbook' )
指明只读字段
可以通过read_only_fields 指明只读字段,即仅用于序列化输出的字段
1 2 3 4 5 6 class BookInfoSerializer (serializers.ModelSerializer): """图书数据序列化器""" class Meta : model = BookInfo fields = ('id' , 'btitle' , 'bpub_date' , 'bread' , 'bcomment' ) read_only_fields = ('id' , 'bread' , 'bcomment' )
1.3.3.3 添加额外参数选项 我们可以使用extra_kwargs 参数为ModelSerializer添加或修改原有的选项参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class BookInfoSerializer (serializers.ModelSerializer): """图书数据序列化器""" class Meta : model = BookInfo fields = ('id' , 'btitle' , 'bpub_date' , 'bread' , 'bcomment' ) extra_kwargs = { 'bread' : {'min_value' : 0 , 'required' : True }, 'bcomment' : {'min_value' : 0 , 'required' : True }, }
扩展 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 class MyDict (dict ): def __setattr__ (self, key, value ): self[key]=value def __getattr__ (self, item ): return self[item] dic = MyDict() dic.name='egon' dic.age=18 print (dic) print (dic.name) fbv(中间件,装饰器) from django.utils.deprecation import MiddlewareMixin import jsonclass DataMiddleware (MiddlewareMixin ): def process_request (self,request ): if request.method == "GET" : pass elif request.method == 'POST' : if request.POST: request.data = request.POST.dict () elif request.body: request.data = json.loads(request.body.decode()) import json def wrapper (func ): def inner (request, *args, **kwargs ): try : request.data = json.loads(request.body) except Exception as e: request.data = request.POST return func(request, *args, **kwargs) return inner @method_decorator(wrapper, name='dispatch' ) class MyView (View ): @method_decorator(csrf_exempt ) def dispatch (self, request, *args, **kwargs ): return super ().dispatch(request, *args, **kwargs) 3 使用模型类序列化器实现publish单表的查询所有和创建一条 -查询字段名name_detail -新增字段名 name 4 高级:实现图书和出版社多表的查询所有和创建一条(部分人做)from .models import Publish,Bookfrom rest_framework.serializers import ModelSerializer,SerializerMethodFieldfrom rest_framework import serializersclass BookSerializer (ModelSerializer ): class Meta : model=Book fields='__all__' extra_kwargs = {"publish" : {'write_only' : True },"authors" : {'write_only' : True }} publish_name=serializers.CharField(source='publish.name' ,read_only=True ) authors_name=SerializerMethodField(source='authors' ,read_only=True ) def get_authors_name (self,obj ): tmp = [] for author in obj.authors.all (): tmp.append(author.name) return tmp class BookView (APIView ): def get (self, request ): obj_list = Book.objects.all () obj_ser = BookSerializer(instance=obj_list, many=True ) return APIResponse(data=obj_ser.data) def post (self, request ): obj_ser = BookSerializer(data=request.data) if obj_ser.is_valid(): obj_ser.save() return APIResponse(data=obj_ser.data) else : return APIResponse(code=101 , msg=obj_ser.errors) class BookDetailView (APIView ): def get (self, request, pk ): obj = Book.objects.filter (pk=pk).first() obj_ser = BookSerializer(instance=obj) return APIResponse(data=obj_ser.data) def put (self, request, pk ): obj = Book.objects.filter (pk=pk).first() obj_ser = BookSerializer(instance=obj, data=request.data) if obj_ser.is_valid(): obj_ser.save() return APIResponse(data=obj_ser.data) return APIResponse(obj_ser.data) def delete (self, request, pk ): Book.objects.filter (pk=pk).delete() return APIResponse(msg='删除成功' )
1.4 局部全局校验源码分析 1 #is_valid ---->self .run_validation-(执行Serializer的run_validation)--> self.to_internal_value(data )---(执行Serializer的run_validation:485 行)
1.5 序列化组件源码分析 1 2 3 4 5 序列化组件,先调用__new__方法,如果many=True ,生成ListSerializer对象,如果为False ,生成Serializer对象 序列化对象.data 方法--调用父类data 方法---调用对象自己的to_representation(自定义的序列化类无此方法,去父类找) Aerializer类里有to_representation方法,for循环执行attribute = field.get_attribute(instance) 再去Field类里去找get_attribute方法,self .source_attrs就是被切分的source,然后执行get_attribute方法,source_attrs 当参数传过去,判断是方法就加括