day86
视图组件
基于以往我们所用的视图函数,我们发现其中冗余的代码比较多,今天就来对其进行封装,争取做一个代码洁癖者
原来我们的视图函数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Book(APIView): def get(self, request): book = models.Books.objects.all() book_ser = Ser.Books(book, many=True) return JsonResponse(book_ser.data, safe=False) def post(self, request): new_book = Ser.Books(data=request.data) if new_book.is_valid(): new_book.save() return JsonResponse(new_book.data) else: return JsonResponse(new_book.errors) class BookDetail(APIView): def get(self, request, pk): book = models.Books.objects.filter(pk=pk).first() book_ser = Ser.Books(book, many=False) return JsonResponse(book_ser.data, safe=False) def put(self, request,pk): book = models.Books.objects.filter(pk=pk).first() new_book = Ser.Books(data=request.data,instance=book) if new_book.is_valid(): new_book.save() return JsonResponse(new_book.data) else: return JsonResponse(new_book.errors)
手动一层封装:
创建了List和Create辅助类,节省了代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class List: def list(self, request): queryset = self.query info = self.serializers(queryset, many=True) return JsonResponse(info.data, safe=False) class Create: def create(self, request): new = self.serializers(data=request.data) if new.is_valid(): new.save() return JsonResponse(new.data, safe=False) else: return JsonResponse(new.errors) class Book(APIView, List, Create): query = models.Books.objects.all() serializers = Ser.Books def get(self, request): return self.list(request) def post(self, request): return self.create(request) class Publish(APIView,List,Create): query = models.Publish.objects.all() serializers = Ser.Publish def get(self, request): return self.list(request) def post(self, request): return self.create(request)
使用drf自带的二层封装:
说明:
''' CreateModelMixin: 增加 DestroyModelMixin:删 UpdateModelMixin:改 ListModelMixin:查全部 RetrieveModelMixin:查一条 '''
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from rest_framework.generics import GenericAPIView from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin, UpdateModelMixin, \ RetrieveModelMixin class Book(GenericAPIView, ListModelMixin, CreateModelMixin): queryset = models.Books.objects.all() serializer_class = Ser.Books def get(self, request): return self.list(request) def post(self, request): return self.create(request) class Publish(GenericAPIView, ListModelMixin, CreateModelMixin): queryset = models.Publish.objects.all() serializer_class = Ser.Publish def get(self, request): return self.list(request) def post(self, request): return self.create(request) class BookDetail(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin): queryset = models.Books.objects.all() serializer_class = Ser.Books def get(self, request, pk): return self.retrieve(request, pk) def put(self, request, pk): return self.update(request, pk) def delete(self, request, pk): return self.destroy(request, pk)
第三层封装
drf中自己写了一个类继承了封装的类里面写了get、post等方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from rest_framework.generics import RetrieveDestroyAPIView, ListCreateAPIView # class Book(GenericAPIView, ListModelMixin, CreateModelMixin): class Book(ListCreateAPIView): queryset = models.Books.objects.all() serializer_class = Ser.Books # class BookDetail(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin): class BookDetail(RetrieveDestroyAPIView): queryset = models.Books.objects.all() serializer_class = Ser.Books
最终的封装(不建议使用)
说明:
''' 终极封装是因为这个类继承了CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,ListModelMixin,GenericViewSet): 注:在路由中配置时需要传递key:value对应的方法参数,保证对应的方法有对应的参数 {'get': 'retrieve', 'put': 'update', 'delete': 'destroy'} '''
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from rest_framework.viewsets import ModelViewSet from rest_framework.renderers import JSONOpenAPIRenderer, BrowsableAPIRenderer, HTMLFormRenderer class Book(ModelViewSet): # renderer_classes = [JSONOpenAPIRenderer, BrowsableAPIRenderer, HTMLFormRenderer] queryset = models.Books.objects.all() serializer_class = Ser.Books class Publish(ModelViewSet): queryset = models.Publish.objects.all() serializer_class = Ser.Publish
魔法方法(重点)
# 建议用魔法类ViewSetMixin from rest_framework.viewsets import ViewSetMixin