GenericAPIView:
源码:
class GenericAPIView(views.APIView): queryset = None serializer_class = None lookup_field = 'pk' lookup_url_kwarg = None filter_backends = api_settings.DEFAULT_FILTER_BACKENDS pagination_class = api_settings.DEFAULT_PAGINATION_CLASS def get_queryset(self) def get_object(self) def get_serializer(self, *args, **kwargs) def get_serializer_class(self) def get_serializer_context(self) def filter_queryset(self, queryset) @property def paginator(self) def paginate_queryset(self, queryset) def get_paginated_response(self, data)
rest framework 的 APIView 是继承 Django 的 view,而 GenericAPIView 是继承 APIView
views.py:
from rest_framework import serializers from rest_framework.generics import GenericAPIView from rest_framework.pagination import PageNumberPagination from drf import models from rest_framework.response import Response class PagerSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = "__all__" class TestView(GenericAPIView): # 看源码 queryset = models.Role.objects.all() # 数据 serializer_class = PagerSerializer # 序列化 pagination_class = PageNumberPagination # 分页 def get(self, request, *args, **kwargs): # 获取数据 roles = self.get_queryset() # 分页 role_page = self.paginate_queryset(roles) # 序列化 ser = self.get_serializer(instance=role_page, many=True) return Response(ser.data)
运行结果:
GenericViewSet:
源码:
它继承了 ViewSetMixin 和 GenericAPIView
urls.py:
from django.urls import path, re_path from drf import views urlpatterns = [ re_path('view/$', views.TestView.as_view({"get": "list"})), ]
TestView 中写的就不是 get 函数,而是 list 的函数,即 get 请求的时候执行 TestView 中的 list 函数
from rest_framework.viewsets import GenericViewSet from rest_framework import serializers from rest_framework.pagination import PageNumberPagination from drf import models from rest_framework.response import Response class PagerSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = "__all__" class TestView(GenericViewSet): # 看源码 queryset = models.Role.objects.all() # 数据 serializer_class = PagerSerializer # 序列化 pagination_class = PageNumberPagination # 分页 def list(self, request, *args, **kwargs): # 获取数据 roles = self.get_queryset() # 分页 role_page = self.paginate_queryset(roles) # 序列化 ser = self.get_serializer(instance=role_page, many=True) return Response(ser.data)
运行结果:
ModelViewSet:
源码:
CreateModelMixin:创建数据,其中有 create 方法
RetrieveModelMixin:显示单条数据,其中有 retrieve 方法
UpdateModelMixin:更新数据,其中有 update、partial_update 方法
DestroyModelMixin:删除数据,其中有 destroy 方法
ListModelMixin:列表显示数据,其中有 list 方法
RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin 需要指定 id 值,而 CreateModelMixin、ListModelMixin 不需要指定 id 值
urls.py:
from django.urls import path, re_path from drf import views urlpatterns = [ re_path('view/(?P<pk>d+)/$', views.TestView.as_view({ "get": "retrieve", "delete": "destroy", "put": "update", "patch": "partial_update", })), ]
views.py:
from rest_framework.viewsets import ModelViewSet from rest_framework import serializers from rest_framework.pagination import PageNumberPagination from drf import models class PagerSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = "__all__" class TestView(ModelViewSet): queryset = models.Role.objects.all() # 数据 serializer_class = PagerSerializer # 序列化 pagination_class = PageNumberPagination # 分页
运行结果:
显示单条数据,也可以进行删除
也可以添加数据
也可以使用多继承来继承某几个视图
多继承:
urls.py:
from django.urls import path, re_path from drf import views urlpatterns = [ path('view/', views.TestView.as_view({"get": "list", "post": "create"})), ]
views.py:
from rest_framework.mixins import ListModelMixin, CreateModelMixin from rest_framework.viewsets import GenericViewSet from rest_framework import serializers from rest_framework.pagination import PageNumberPagination from drf import models class PagerSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = "__all__" class TestView(ListModelMixin, CreateModelMixin, GenericViewSet): queryset = models.Role.objects.all() # 数据 serializer_class = PagerSerializer # 序列化 pagination_class = PageNumberPagination # 分页