看一下优化以前的代码

  1. from rest_framework.generics import GenericAPIView
  2. from django_filters.rest_framework import DjangoFilterBackend
  3. from rest_framework.filters import OrderingFilter
  4. from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
  5. from utils.pagination import NewPagination
  6. from .models import Projects
  7. from .serializers import ProjectsModelSerializer
  8. class ProjectsPage(ListModelMixin,
  9. CreateModelMixin,
  10. GenericAPIView):
  11. '''
  12. 类视图
  13. '''
  14. queryset = Projects.objects.all()
  15. serializer_class = ProjectsModelSerializer
  16. filter_backends = [DjangoFilterBackend, OrderingFilter]
  17. filterset_fields = ['id', 'name', 'leader', 'programmer', 'tester']
  18. ordering_fields = ['id', 'name', 'leader', 'programmer', 'tester']
  19. pagination_class = NewPagination
  20. def get(self, request, *args, **kwargs):
  21. return self.list(request, *args, **kwargs)
  22. def post(self, request, *args, **kwargs):
  23. return self.create(request, *args, **kwargs)
  24. class ProjectsDetailsPage(RetrieveModelMixin,
  25. UpdateModelMixin,
  26. DestroyModelMixin,
  27. GenericAPIView):
  28. queryset = Projects.objects.all()
  29. serializer_class = ProjectsModelSerializer
  30. def get(self, request, *args, **kwargs):
  31. self.retrieve(request, *args, **kwargs)
  32. def put(self, request, *args, **kwargs):
  33. self.update(request, *args, **kwargs)
  34. def delete(self, request, *args, **kwargs):
  35. return self.destroy(request, *args, **kwargs)

本次优化只涉及当前视图,一般一个项目有多个视图,如果结构上相似,是否可以不要每次读进行cv这些方法呢。
本章通过generics模块中的一系列类来继续优化视图当中的请求方法。先看一下generics的类结构图
DRF-Concrete Generic Views - 图1

Generics模块下主要有下面的类来做优化

  • CreateAPIView:继承了mixins.CreateModelMixin和GenericAPIView
  • ListAPIView:继承了mixins.ListModelMixin和GenericAPIView
  • RetrieveAPIView:继承了mixins.RetrieveModelMixin和GenericAPIView
  • DestroyAPIView:继承了mixins.DestroyModelMixin和GenericAPIView
  • UpdateAPIView:继承了mixins.UpdateModelMixin和GenericAPIView
  • ListCreateAPIView:继承了mixins.ListModelMixin、mixins.CreateModelMixin和GenericAPIView
  • RetrieveUpdateAPIView:继承了mixins.RetrieveModelMixin、mixins.UpdateModelMixin和GenericAPIView
  • RetrieveDestroyAPIView:继承了mixins.RetrieveModelMixin、mixins.DestroyModelMixin和GenericAPIView
  • RetrieveUpdateDestroyAPIView:继承了mixins.RetrieveModelMixin、mixins.UpdateModelMixin、mixins.DestroyModelMixin和GenericAPIView

    由于ProjectsPage视图类有获取项目列表信息的get请求和创建项目的post请求,因此可以继承ListCreateAPIView类来进行优化

    1. class ProjectsPage(ListCreateAPIView):
    2. '''
    3. 类视图
    4. '''
    5. queryset = Projects.objects.all()
    6. serializer_class = ProjectsModelSerializer
    7. filter_backends = [DjangoFilterBackend, OrderingFilter]
    8. filterset_fields = ['id', 'name', 'leader', 'programmer', 'tester']
    9. ordering_fields = ['id', 'name', 'leader', 'programmer', 'tester']
    10. pagination_class = NewPagination
  • ProjectsDetailsPage视图类有获取项目详情的get请求,更新的put请求和删除项目的delete请求,因此可以继承RetrieveUpdateDestroyAPIView类来进行优化

    1. class ProjectsDetailsPage(RetrieveUpdateDestroyAPIView):
    2. queryset = Projects.objects.all()
    3. serializer_class = ProjectsModelSerializer