1. 程式人生 > >Django詳解之檢視

Django詳解之檢視

一、繼承apiview

# class BookDetailView(APIView):
# 
# 
#     def get(self, request):
#         # 獲取全部
#         books = BookInfo.objects.all()
#         s = BookInfoSerializer(books, many=True)
#         return Response(s.data)
# 
# 
#     def get(self, request, pk):
#         # 獲取單個
#         book = BookInfo.objects.get(id=pk)
#         s = BookInfoSerializer(book)
#         return Response(s.data)
#     
#     
#     def get(self, request):
#         # 建立一個物件()
#         date = {'btitle': "高數", 'bput_date': "2018-01-01"}
#         s = BookInfoSerializer(data=date)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)
#     
#     
#     def get(self, request):
#         # 更新一個物件
#         data = {"btitle": "巴西柔術"}
#         book = BookInfo.objects.get(btitle='高數')
#         s = BookInfoSerializer(book, data=data, partial=True)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)

二、繼承GenericAPIView

# class BookDetailView(GenericAPIView):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
# 
#     def get(self, request):
#         # 獲取全部資料
#         books = self.get_queryset()
#         s = self.get_serializer(books, many=True)
#         return Response(s.data)
# 
#     def get(self, request, pk):
#         # 獲取單一資料
#         book = self.get_object()
#         s = self.get_serializer(book)
#         return Response(s.data)
# 
#     def get(self, request):
#         # 增加一條資料
#         data = {'btitle': '泰拳', 'bput_date': '2018-01-01'}
#         s = self.get_serializer(data=data)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)
# 
#     def get(self, request):
#         data = {'btitle': '桑巴'}
#         book = BookInfo.objects.get(btitle='泰拳')
#         s = self.get_serializer(book, data=data, partial=True)
#         s.is_valid(raise_exception=True)
#         s.save()
#         return Response(s.validated_data)

三、GenericAPIView五個拓展類使用

# class BookDetailView(CreateModelMixin,ListModelMixin,GenericAPIView):
#     # GenericAPIView拓展類查詢列表和建立
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
#     def get(self,request):
#         return self.list(request)
#     def post(self,request):
#         return self.create(request)
# 
# class BookDetailView(DestroyModelMixin, UpdateModelMixin, RetrieveModelMixin, GenericAPIView):
#     # GenericAPIView拓展類單個物件和修改資料(???????partial總是false),刪除
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer
# 
#     def get(self, request, pk):
#         return self.retrieve(request)
# 
#     def post(self, request, pk):
#         return self.update(request, content={"partial": True})
# 
#     def put(self, request, pk):
#         return self.destroy(request)

四、GenericAPIView和拓展類的9種組合類(寫了4種)

class BookDetailView(CreateAPIView):
    # GenericAPIView拓展類查詢列表和建立
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer


class BookDetailView(ListAPIView):
    # GenericAPIView拓展類查詢列表和建立
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer


class BookDetailView(RetrieveAPIView):
    # GenericAPIView拓展類查詢列表和建立
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer


class BookDetailView(DestroyAPIView):
    # GenericAPIView拓展類查詢列表和建立
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

五、檢視結果集ViewSet

路由:

url(r'(P<pk>.*)/$',views.BookDetailView.as_view({'get':'retrieve','put':'update','delete':'destory'})),
url(r'^',views.BookDetailView.as_view({'get':'list','post':'create'})),

檢視:

class BookDetailView(ViewSet):
    def list(self, request):
        return HttpResponse('list')

    def retrieve(self, request, pk):
        return HttpResponse('retrieve')

    def create(self, request):
        return HttpResponse('create')

    def update(self, request, pk):
        return HttpResponse('update')

    def destory(self, request, pk):
        return HttpResponse('destory')

六、GenericViewSet配合5個拓展類用法:

路由和ViewSet一樣

檢視:

class BookDetailView(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, ListModelMixin,
                     GenericViewSet):
    # GenericViewSet配合拓展類
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

七、ModelViewSet(GenericViewSet的拓展類,繼承GnericViewSet和5個拓展類)

class BookInfoViewSet(ModelViewSet):
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    @action(methods=['get'], detail=False)
    def latest(self, request):
        book = BookInfo.objects.latest('id')
        s = self.get_serializer(book)
        return Response(s.data)