在视图中,如果参数有request,我们可以直接使用,如果参数没有,则可以通过self.request获取
1.APIView
我们之前使用的基本上都是APIView,给我们提供了免除csrf验证,请求封装,版本控制,认证,权限和限流的功能,以下是源码展示
2.GenericAPIView
GenericAPIView 继承了APIView,增加了一些功能,如get_queryset,get_object等,实际开发中我们不会直接继承该类,该类是为后续的高度封装做的准备
3.
GenericViewSet
类中没有定义任何代码,他就是继承 ViewSetMixin
和 GenericAPIView
,也就说他的功能就是将继承的两个类的功能继承到一起。
-
GenericAPIView
-
ViewSetMixin
,将 get/post/put/delete 等方法映射到 list、create、retrieve、update、partial_update、destroy方法中,让视图不再需要两个类。
# urls.py from django.urls import path, re_path, include from app01 import views urlpatterns = [ path('api/users/', views.UserView.as_view({"get":"list","post":"create"})), path('api/users/<int:pk>/', views.UserView.as_view({"get":"retrieve","put":"update","patch":"partial_update","delete":"destory"})), ]
# views.py from rest_framework.viewsets import GenericViewSet from rest_framework.response import Response class UserView(GenericViewSet): # 认证、权限、限流等 queryset = models.UserInfo.objects.filter(status=True) serializer_class = 序列化类 def list(self, request): # 业务逻辑:查看列表 queryset = self.get_queryset() ser = self.get_serializer(intance=queryset,many=True) print(ser.data) return Response({"code": 0, 'data': "..."}) def create(self, request): # 业务逻辑:新建 return Response({'code': 0, 'data': "..."}) def retrieve(self, request,pk): # 业务逻辑:查看某个数据的详细 return Response({"code": 0, 'data': "..."}) def update(self, request,pk): # 业务逻辑:全部修改 return Response({'code': 0, 'data': "..."}) def partial_update(self, request,pk): # 业务逻辑:局部修改 return Response({'code': 0, 'data': "..."}) def destory(self, request,pk): # 业务逻辑:删除 return Response({'code': 0, 'data': "..."})
4.1 ListModelMixin
查看数据列表
路由
from django.urls import path from app01 import views urlpatterns = [ path('api/user/', views.User.as_view({'get': 'list'})), ]
视图
from app01.models import UserInfo from rest_framework import serializers from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, \ RetrieveModelMixin, \ CreateModelMixin, \ UpdateModelMixin, \ DestroyModelMixin class UserModelSerializer(serializers.ModelSerializer): class Meta: model = UserInfo fields = ['username', 'age', 'email'] class User(ListModelMixin, GenericViewSet): queryset = UserInfo.objects.all() serializer_class = UserModelSerializer
4.2 RetrieveModelMixin
查看指定数据
路由
urlpatterns = [ path('api/user/<int:pk>/', views.User.as_view({'get': 'retrieve'})), ]
视图
from app01.models import UserInfo from rest_framework import serializers from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, \ RetrieveModelMixin, \ CreateModelMixin, \ UpdateModelMixin, \ DestroyModelMixin class UserModelSerializer(serializers.ModelSerializer): class Meta: model = UserInfo fields = ['username', 'age', 'email'] class User(ListModelMixin, RetrieveModelMixin, GenericViewSet): queryset = UserInfo.objects.all() serializer_class = UserModelSerializer
4.3 CreateModelMixin
创建数据
我们在保存数据到数据库中,可能前端传过来的数据只是部分,我们还需要对其他字段进行指定,可以重写perform_create
路由
urlpatterns = [ path('api/user/', views.User.as_view({'get': 'list', 'post': 'create'})), ]
视图
from app01.models import UserInfo from rest_framework import serializers from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, \ RetrieveModelMixin, \ CreateModelMixin, \ UpdateModelMixin, \ DestroyModelMixin class UserModelSerializer(serializers.ModelSerializer): class Meta: model = UserInfo fields = ['username', 'age', 'email'] class User(ListModelMixin, RetrieveModelMixin, CreateModelMixin, GenericViewSet): queryset = UserInfo.objects.all() serializer_class = UserModelSerializer def perform_create(self, serializer): serializer.save(password="123")
4.4 DestroyModelMixin
删除数据
路由
urlpatterns = [ path('api/user/<int:pk>/', views.User.as_view({'get': 'retrieve', 'delete': 'destroy'})), ]
视图
from app01.models import UserInfo from rest_framework import serializers from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, \ RetrieveModelMixin, \ CreateModelMixin, \ UpdateModelMixin, \ DestroyModelMixin class UserModelSerializer(serializers.ModelSerializer): class Meta: model = UserInfo fields = ['username', 'age', 'email'] class User(ListModelMixin, RetrieveModelMixin, CreateModelMixin, DestroyModelMixin, GenericViewSet): queryset = UserInfo.objects.all() serializer_class = UserModelSerializer
4.5 UpdateModelMixin
路由
from django.urls import path from app01 import views urlpatterns = [ path('api/user/', views.User.as_view({'get': 'list', 'post': 'create'})), path('api/user/<int:pk>/', views.User.as_view( {'get': 'retrieve', 'delete': 'destroy', 'put': 'update', 'patch': 'partial_update' } ) ), ]
视图
from app01.models import UserInfo from rest_framework import serializers from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, \ RetrieveModelMixin, \ CreateModelMixin, \ UpdateModelMixin, \ DestroyModelMixin class UserModelSerializer(serializers.ModelSerializer): class Meta: model = UserInfo fields = ['username', 'age', 'email'] class User(ListModelMixin, RetrieveModelMixin, CreateModelMixin, DestroyModelMixin, UpdateModelMixin, GenericViewSet): queryset = UserInfo.objects.all() serializer_class = UserModelSerializer
小结:
-
接口与数据库操作无关,直接继承APIView
-
接口背后需要对数据库进行操作,一般:
ModelViewSet
或CreateModelMixin、ListModelMixin...
-
利用钩子自定义功能。重写某个写方法,实现更加完善的功能。