GenericAPIView简单学习

1、继承自APIView

2、提供的函数方法

3、要在类中定义属性queryset和serializer_class,之后使用对应方法时用到

queryset:指明当前视图处理数据默认使用的数据集

serializer_class:指明当前视图处理数据默认使用的序列器类

lookup_field='pk':  默认值就是‘pk’,根据pk字段进行过滤

lookup_url_kwarg='id':根据说明字段在分组中提取参数,默认为lookup_field

patch中get_serializer(book,data=book_info,partial=True)

这样就会更新一个字段,不用传所有必传参数才能更新

4、五个扩展类

from rest_framework.mixins import ListModelMixin,CreateModelMixin

ListModelMixin里面的list方法:序列化返回多条数据,继承自object;查看源码,使用了get_queryset方法,不是继承,是使用方法。get请求

CreateModelMixin里面的create方法:新建保存数据;post请求

RetrieveModelMixin里面的retrieve方法:序列化单一数据对象;get请求

UpdateModelMixin里面的update方法:全更新;put请求

           partial_update:部分更新;patch请求

destroyModelMixin里面的destroy方法和perform_destroy方法:删除;delete请求

5、五个子类

ListAPIView(mixins.ListModelMixin,GenericAPIView):get方法调用list方法,序列化返回多条数据,响应GET请求

CreateAPIView(mixins.CreateModelMixin,GenericAPIView):post方法调用create方法,新建数据,响应POST请求

RetrieveAPIView(mixins.RetrieveModelMixin,GenericAPIView):get方法调用retrieve方法,序列化返回单一数据,响应GET请求

UpdateAPIView(mixins.UpdateModelMixin,GenericAPIView):put方法调用update方法,全更新对应数据,响应PUT请求

                           patch方法调用partial_update方法,部分更新数据,响应patch请求

DestroyAPIView(mixins.DestroyModelMixin,GenericAPIView):delete方法调用destroy方法,单一数据删除,响应delete请求

注:单一数据都有一个参数进行过滤,默认是PK(主键id)

猜你喜欢

转载自www.cnblogs.com/bestsong/p/11142163.html
今日推荐