目录
视图层总结
一、两个视图基类
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
- APIView
- GenericAPIView
二、五个视图扩展类
from rest_framework.generics import GenericAPIView # 一定要配合GenericAPIView使用
from rest_framework.mixins import ListModelMixin, CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin
- 新增数据,CreateModelMixin
- 修改数据,UpdateModelMixin
- 删除数据,DestroyModelMixin
- 获取单个,RetrieveModelMixin
- 获取所有,ListModelMixin
三、九个视图子类
from rest_framework.generics import CreateAPIView, DestroyAPIView, ListAPIView, ListCreateAPIView, RetrieveAPIView, RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView, UpdateAPIView
- 查询所有 ListAPIView
- 新增数据 CreateAPIView
- 删除数据 DestroyAPIView
- 修改数据 UpdateAPIView
- 查询单个 RetrieveAPIView
- 查询所有新增数据 ListCreateAPIView
- 查询单个删除数据 RetrieveDestroyAPIView
- 查询单个修改数据 RetrieveUpdateAPIView
- 查询单个修改删除数据 RetrieveUpdateDestroyAPIView
四、 视图集
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet,ViewSetMixin
- ModelViewSet
- ReadOnlyModelViewSet
- ViewSetMixin
- ViewSet
- GenericViewSet
# 继承视图集那就路由写法变了 变成映射的方式
urlpatterns = [
path('books/', views.BookView.as_view({'get': 'list', 'post': 'create'})),
]
五、总结
标签:总结,GenericAPIView,rest,查询,framework,视图,import From: https://www.cnblogs.com/almira998/p/17112549.html
- 以后,你想继承APIView,但是想变路由写法【视图类中方法名任意命名】,要继承ViewSet
- 以后,你想继承GenericAPIView,但是想变路由写法【视图类中方法名任意命名】,要继承GenericViewSet