视图类用法大总结
Posted skyoceanchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视图类用法大总结相关的知识,希望对你有一定的参考价值。
视图家族
from rest_framework import views, generics, mixins, viewsets
views:视图类
mixins:视图工具类
generics:工具视图类
viewsets:视图集
#俩大视图类:
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
# APIView,GenericAPIView
# GenericAPIView:是所有工具视图的父类
# 六大视图工具类:
from rest_framework.mixins import RetrieveModelMixin,ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin
# RetrieveModelMixin,单查询
# ListModelMixin,群查询
# CreateModelMixin,增加
# UpdateModelMixin,更新,在这里面有两种方法,因此是六大视图工具类
# DestroyModelMixin,删除
# 九大工具视图类
from rest_framework.generics import CreateAPIView,DestroyAPIView,GenericAPIView,ListAPIView,ListCreateAPIView,RetrieveAPIView,RetrieveDestroyAPIView,RetrieveUpdateAPIView,RetrieveUpdateDestroyAPIView,UpdateAPIView
# CreateAPIView,单增加
# DestroyAPIView,删除
# GenericAPIView,基类
# ListAPIView,群查
# ListCreateAPIView,群查,单增加
# RetrieveAPIView,单查
# RetrieveDestroyAPIView,
# RetrieveUpdateAPIView,
# RetrieveUpdateDestroyAPIView,
# UpdateAPIView
#两大视图集基类
from rest_framework.viewsets import ViewSet,GenericViewSet
# ViewSet,
# GenericViewSet
数据准备
from django.db import models
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=64)
APIView
from rest_framework.views import APIView
APIView继承Django的View
1) View:将请求方式与视图类的同名方法建立映射,完成请求响应
2) APView:
主要继承了View的功能;
重写as_view禁用csrf认证;
重写dispatch:请求、响应、渲染、异常、解析、三大认证
多了一堆类属性,可以完成视图类的局部配置
from . import models, serializers
from rest_framework.response import Response
class StudentAPIView(APIView):
def get(self, request, *args, **kwargs):
stu_query = models.Student.objects.all()
stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
return Response(stu_ser.data)
url(r'^v1/students/$', views.StudentAPIView.as_view()),
url(r'^v1/students/(?P<pk>d+)/$', views.StudentAPIView.as_view()),
GenericAPIView
from rest_framework.generics import GenericAPIView
class GenericAPIView(views.APIView):#继承了views.APIView的所有功能
# GenericAPIView提供了三个方法:get_object()、get_queryset()、get_serializer()
class StudentGenericAPIView(GenericAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def get(self, request, *args, **kwargs):
# stu_query = models.Student.objects.all()
stu_query = self.get_queryset()
# stu_ser = serializers.StudentModelSerializer(stu_query, many=True)
stu_ser = self.get_serializer(stu_query, many=True)
return Response(stu_ser.data)
url(r'^v2/students/$', views.StudentGenericAPIView.as_view()),
url(r'^v2/students/(?P<pk>d+)/$', views.StudentGenericAPIView.as_view()),
视图工具类
from rest_framework import mixins
class StudentMixinGenericAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
url(r'^v3/students/$', views.StudentMixinGenericAPIView.as_view()),
url(r'^v3/students/(?P<pk>d+)/$', views.StudentMixinGenericAPIView.as_view()),
工具视图类(重点)
主要是查询是最重要的
单查询和群查询不能同时使用
# 工具视图类
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView
class StudentMixinAPIView(CreateAPIView, RetrieveAPIView, ListAPIView, UpdateAPIView, DestroyAPIView):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
# url中单查,不一定必须提供主键,提供一切唯一键的字段名均可
lookup_url_kwarg = 'id'
# 有删除需求的接口继承DestroyAPIView,重写destroy完成字段删除
def destroy(self, request, *args, **kwargs):
pass
url(r'^v4/students/$', views.StudentMixinAPIView.as_view()),
url(r'^v4/students/(?P<id>d+)/$', views.StudentMixinAPIView.as_view()),
# 将所有 请求方式 与 响应方法名 的映射关系交给用户自己配置
视图集
可以解决单查和群查询
from rest_framework.viewsets import ModelViewSet
class StudentModelViewSet(ModelViewSet):
queryset = models.Student.objects.all()
serializer_class = serializers.StudentModelSerializer
def my_post(self, request, *args, **kwargs):
return Response('my post ok')
url(r'^v5/students/$', views.StudentModelViewSet.as_view({
'get': 'list',
'post': 'my_post'
})),
url(r'^v5/students/(?P<pk>d+)/$', views.StudentModelViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
})),
以上是关于视图类用法大总结的主要内容,如果未能解决你的问题,请参考以下文章