一、DRF框架之五个扩展类视图的子类视图
常用子类视图
1.CreateAPIView
提供 post 方法
继承自: GenericAPIView、CreateModelMixin
2.ListAPIView
提供 get 方法
继承自:GenericAPIView、ListModelMixin
3.RetireveAPIView
提供 get 方法
继承自: GenericAPIView、RetrieveModelMixin
4.DestoryAPIView
提供 delete 方法
继承自:GenericAPIView、DestoryModelMixin
5.UpdateAPIView
提供 put 和 patch 方法
继承自:GenericAPIView、UpdateModelMixin
6.ListCreateAPIView
提供 get、post 方法
继承自:GenericAPIView、ListModelMixin、CreateModelMixin
7.RetrieveDestroyAPIView
提供get、delete方法
继承自:GenericAPIView、RetrieveModelMixin、DestoryModelMixin
8.RetrieveUpdateAPIView
提供 get、put、patch方法
继承自: GenericAPIView、RetrieveModelMixin、UpdateModelMixin
9.RetrieveUpdateDestoryAPIView
提供 get、put、patch、delete方法
继承自:GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestoryModelMixin
二、相关案例
from rest_framework.response import Response from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \ DestroyModelMixin from rest_framework.generics import GenericAPIView from book.models import BookInfo from drf_book.serializer import BookSerializer from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView class BooksMixinView(ListCreateAPIView): """ 获取所有图书和保存图书 """ # 指定查询集属性 queryset = BookInfo.objects.all() # 指定序列化器 serializer_class = BookSerializer class BookMixinView(RetrieveUpdateDestroyAPIView): """ 获取单一图书数据 更新图书 删除图书 """ # 指定查询集属性 queryset = BookInfo.objects.all() # 指定序列化器 serializer_class = BookSerializer
三、序列化器进一步封装使用
1.封装序列化器的常用方法
# 源代码封装基本序列化器 class BaseSerialzier(object): def __init__(self, instance=None, data=None): """ 初始化方法 :param instance: 对象 :param data: 验证的数据 """ self.instance = instance self.v_data = data def is_valid(self): """ 验证方法 :return: """ self.validate_data = self.validate(self.v_data) return self.validate_data def save(self): """ 保存或更新 :return: """ if self.instance is not None: self.instance = self.update(self.instance, self.validate_data) else: self.instance = self.create(self.validate_data) return self.instance @property def data(self): """ 获取序列化结果方法 :return: """ return {'btitle': 'python'} def validate(self, atter): """ 多个字段验证 :return: """ pass def create(self, validate_data): """ 保存数据 :return: """ pass def update(self, instance, validate_data): """ 更新数据 :return: """ pass
2.序列化器的定义
from baseserilaizer import BaseSerialzier class BookSerializer(BaseSerialzier): """ 自定义序列化器 """ def validate(self, atter): # 字段验证逻辑 print(atter) return atter def update(self, instance, validate_data): print('update') print(validate_data) return instance def create(self, validate_data): print('create') return validate_data
3.视图定义使用序列化器
from serializer import BookSerializer from genericapiview import GenericAPIView class BookView(GenericAPIView): queryset = {'btitle':'python'} serializer_class = BookSerializer def post(self,request): # 1、获取前端数据 data=request.get('data') # 2、验证数据 ser=self.get_serializer(data=data) ser.is_valid() # 3、保存数据 ser.save() # 4、返回结果 return ser.data request={'data':{'btitle':'python'}} data=BookView().post(request) print(data)
总结
子类视图总共有9个如下:
CreateAPIView:继承CreateModelMixin,GenericAPIView,有post方法,新增数据
DestroyAPIView:继承DestroyModelMixin,GenericAPIView,有delete方法,删除数据
ListAPIView:继承ListModelMixin,GenericAPIView,有get方法获取所有
UpdateAPIView:继承UpdateModelMixin,GenericAPIView,有put和patch方法,修改数据
RetrieveAPIView:继承RetrieveModelMixin,GenericAPIView,有get方法,获取一条
ListCreateAPIView:继承ListModelMixin,CreateModelMixin,GenericAPIView,有get获取所有,post方法新增
RetrieveDestroyAPIView:继承RetrieveModelMixin,DestroyModelMixin,GenericAPIView,有get方法获取一条,delete方法删除
RetrieveUpdateAPIView:继承RetrieveModelMixin,UpdateModelMixin,GenericAPIView,有get获取一条,put,patch修改
RetrieveUpdateDestroyAPIView:继承RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView,有get获取一条
DRF框架的五个扩展类视图的子类视图,是为了更便于crud的封装减少通用逻辑的代码量,也是DRF简化版本,其实下面还有视图集可以进一步简化代码,等下一节介绍了。
相关DRF框架的API介绍:
获取列表资源list(request)
新建单一资源self.create(request)
获取单一资源self.retrieve(request, pk)
全更新单一资源self.update(request, pk)
部分更新单一资源self.partial_update(request, pk)
删除单一资源self.destroy(request, pk)