drf视图继承关系表
2个视图基类
之前写5个接口,继承了APIview
还可以继承GenericAPIView,它帮我们干了好多事
GenericAPIView:属性和方法
属性
掌握项
-queryset 要序列化的数据
-serializer_class 序列化类
了解项
-lookup_field # 通过get_object 获取单个对象的查询key值,value值是路由中传进来的
-filter_backends # 过滤类
-pagination_class # 分页类
方法
掌握项
-get_queryset 获取要序列化的数据
-get_object 根据lookup_field配置的参数获取单个对象
-get_serializer 获取序列化类 咱们直接用的
-get_serializer_class 获取序列化类 不是咱们直接用的get_serializer调用了它
了解项
-paginate_xxx # 跟分页有关
-filter_queryset # 跟过滤有关系
基于APIView写5个接口
urls.py
from django.contrib import admin
from django.urls import path
from app01.views import PublishView, PublishDetailView
urlpatterns = [
path('admin/', admin.site.urls),
path('publish/', PublishView.as_view()),
path('publish/<int:pk>/', PublishDetailView.as_view())
]
models.py
from django.db import models
# Create your models here.
class Publish(models.Model):
name = models.CharField(max_length=32, verbose_name='出版社名称')
address = models.CharField(max_length=64, verbose_name='出版社地址')
contact_way = models.CharField(max_length=32, verbose_name='联系方式')
views.py
from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND
class PublishView(APIView):
def get(self, request):
publish_list = Publish.objects.all()
info = PublishModelSerializer(instance=publish_list, many=True)
return Response(info.data)
def post(self, request):
info = PublishModelSerializer(data=request.data)
if info.is_valid():
info.save()
return Response({'msg': '新增成功'}, status=HTTP_200_OK)
else:
return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)
class PublishDetailView(APIView):
def get(self, request, pk):
publish = Publish.objects.filter(pk=pk).first()
info = PublishModelSerializer(instance=publish)
return Response(info.data)
def Put(self, request, pk):
book = Publish.objects.filter(pk=pk).first()
info = PublishModelSerializer(instance=Publish, data=request.data)
if info.is_valid():
info.save()
return Response({'msg': '修改成功'}, status=HTTP_200_OK)
else:
return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)
def delete(self, request, pk):
Publish.objects.filter(pk=pk).delete()
return Response({'msg': '删除成功'}, status=HTTP_200_OK)
Serializer.py
from rest_framework import serializers
from .models import Publish
class PublishModelSerializer(serializers.ModelSerializer):
class Meta:
model = Publish
fields = '__all__'
基于GenericAPIView写5个接口
views.py
from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND
from rest_framework.generics import GenericAPIView
class PublishView(GenericAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request):
publish_list = self.get_queryset()
info = self.get_serializer(instance=publish_list, many=True)
return Response(info.data)
def post(self, request):
info = self.get_serializer(data=request.data)
if info.is_valid():
info.save()
return Response({'msg': '新增成功'}, status=HTTP_200_OK)
else:
return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)
class PublishDetailView(GenericAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request, pk):
publish = self.get_object()
info = self.get_serializer(instance=publish)
return Response(info.data)
def Put(self, request, pk):
book = self.get_object()
info = self.get_serializer(instance=Publish, data=request.data)
if info.is_valid():
info.save()
return Response({'msg': '修改成功'}, status=HTTP_200_OK)
else:
return Response({'msg': info.errors}, status=HTTP_404_NOT_FOUND)
def delete(self, request, pk):
self.get_queryset().filter(pk=pk).delete()
return Response({'msg': '删除成功'}, status=HTTP_200_OK)
5个视图扩展类
CreateModelMixin:Create方法,就是咱们原来的post 新增数据
RetrieveModelMixin:retrieve方法,就是咱们原来的get 获取单条数据
UpdateModelMixin:update方法,就是咱们原来的put 修改数据
ListModelMixin:list方法,就是原来咱们的get 获取所有数据
DestroyModelMixin:destroy方法,就是原来咱们的delete 删除数据
### 必须配合GenericAPIView使用,不能配合APIView使用
基于GenericAPIView+5个视图扩展类编写5个接口
views.py
from .models import Publish
from .serializer import PublishModelSerializer
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin, \
ListModelMixin
class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class PublishDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def Put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
基于9个视图子类写接口
视图类:继承了GenericAPIView加上某个或某几个视图扩展类
views.py
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveAPIView, DestroyAPIView, UpdateAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, \
RetrieveUpdateAPIView
class PublishView(ListCreateAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
class PublishDetailView(RetrieveUpdateDestroyAPIView):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
视图集
使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中
list() | 提供一组数据 |
retrieve | 提供单个数据 |
create | 创建数据 |
update | 保存数据 |
destory | 删除数据 |
ViewSet视图集不再实现get(),post()等方法,而是实现动作action 如:list()、create()等
继承ModelViewSet编写5个接口
urls.py
from django.contrib import admin
from django.urls import path
from app01.views import PublishView
urlpatterns = [
path('admin/', admin.site.urls),
path('publish/', PublishView.as_view({'get': 'list', 'post': 'create'})),
path('publish/<int:pk>/', PublishView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}))
]
views.py
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
class PublishView(ModelViewSet):
queryset = Publish.objects.all()
serializer_class = PublishModelSerializer
ViewSetMixin源码分析
from rest_framework.viewsets import ViewSetMixin, ViewSet, GenericViewSet
# ViewSet: APIView+ViewSetMixin的组合
# ViewSetMixin: 控制了路由的写法
# GenericViewSet: GenericAPIView+ViewSetMixin的组
"""
ModelViewSet它继承了很多类,其中GenericViewSet继承了GenericAPIView+ViewSetMixin,而ViewSetMixin方法之所以能够控制路由,就是因为它重写了as_view
所以在路由配置中的as_view,其实已经不是APIview中的as_view了,这里的as_view就是ViewSetMixin方法的as_view
继承ViewSetMixin的as_view,是根据继承的查找顺序执行的 userview>>>>ModelViewSet>>>>GenericAPIView>>>>>ViewSetMixin
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet
"""
class ViewSetMixin:
@classonlymethod
def as_view(cls, actions=None, **initkwargs):
# 路由中as_view中必须传参数,必须传字典:{'get': 'list', 'post': 'create'}
if not actions:
raise TypeError("The `actions` argument must be provided when "
"calling `.as_view()` on a ViewSet. For example "
"`.as_view({'get': 'list'})`")
# 路由匹配成功,执行view(request),request是老的request
def view(request, *args, **kwargs):
# actions={'get': 'list', 'post': 'create'}
for method, action in actions.items():
# method:get action:list
# self 是视图类的对象中通过反射,查找list,
# handler视图类中的list方法
handler = getattr(self, action)
# 向视图类的对象中,反射 method:get,handler:list方法
# self.get=list
setattr(self, method, handler)
return self.dispatch(request, *args, **kwargs)
return csrf_exempt(view)
# 只要继承了ViewSetMixin,路由写法就会变,都要写成:views.UserView.as_view({'get': 'list', 'post': 'create'}))
# 这样写好以后,对应的请求方式来了,就会执行配置的方法
# 扩展:
-以后只要继承了ViewSetMixin,视图类中可以写任意名字的方法,不用非得写get,post,delete
标签:info,get,self,request,学习,import,class,drf
From: https://www.cnblogs.com/zzjjpp/p/16759271.html