Django Rest Framework --- 視圖組件

一:視圖組件的做用app

  在每個視圖類中都寫請求方法,而且書寫邏輯,這樣會大大增長開發時間,所以咱們能夠對這一類利用面向對象的封裝來解決。post

二:手動封裝視圖類url

  view.pyspa

# 獲取所有數據
class List: def list(self, request): query_set_list = self.query_set_list data_serializer = self.ser(query_set_list, many=True) response = {"status": 100, "message": "數據獲取成功"} response['data'] = data_serializer.data return Response(response) # 建立新數據
class Create: def create(self, request): data_serializer = self.ser(data=request.data) # 數據校驗
        if data_serializer.is_valid(): data_serializer.save() response = {"status": 100, "message": "建立用戶成功", "data": data_serializer.data} else: response = {"status": 100, "message": data_serializer.errors, "data": ""} return Response(response) # 獲取一個數據
class GetOne: def get_one(self, request, pk): query_set_obj = self.query_set_list.filter(pk=pk).first() if query_set_obj: data_serializer = self.ser(query_set_obj, many=False) response = {"status": 100, "message": "獲取數據成功", "data": data_serializer.data} return Response(response) else: response = {"status": 100, "message": "該數據不存在", "data":""} return Response(response) #修改數據
class Update: def update(self,request,pk): query_set_obj = self.query_set_list.filter(pk=pk).first() if query_set_obj: data_serializer = self.ser(instance=query_set_obj,data=request.data) if data_serializer.is_valid(): data_serializer.save() response = {"status":100,"message":"數據修改爲功","data":data_serializer.data} else: response = {"status": 100, "message": data_serializer.errors, "data":""} else: response = {"status": 100, "message": "須要修改的數據不存在", "data": ""} return Response(response) #刪除數據
class Delete: def delete_by_id(self,request,pk): query_set_obj = self.query_set_list.filter(pk=pk).first() if query_set_obj: query_set_obj.delete() response = {"status": 100, "message": "數據刪除成功", "data": ""} else: response = {"status": 100, "message": "須要刪除的數據不存在", "data": ""} return Response(response) class BooksView(List, Create, APIView): query_set_list = models.Book.objects.all() ser = BookSerializers def get(self, request): return self.list(request) def post(self, request): return self.create(request) class BookView(GetOne,Update,Delete,APIView): query_set_list = models.Book.objects.all() ser = BookSerializers def get(self, request, pk): return self.get_one(request,pk) def put(self,request,pk): return self.update(request,pk) def delete(self,request,pk): return self.delete_by_id(request,pk)

  urls.pyrest

from app01.views import BooksView,BookView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Books/$', BooksView.as_view()), url(r'^Books/(?P<pk>\d+)$', BookView.as_view()), ]

三:restframework視圖組件之mixin類和generice類編寫視圖code

本質就是上面的封裝視圖過程,在返回數據方面有一些不一樣。對象

 views.pyblog

from rest_framework.mixins import CreateModelMixin,UpdateModelMixin,ListModelMixin,DestroyModelMixin,RetrieveModelMixin from rest_framework.generics import GenericAPIView #查詢全部數據和建立新數據的視圖
class BooksView(ListModelMixin,CreateModelMixin,GenericAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers def get(self,request,*args,**kwargs): return self.list(request,*args,**kwargs) def post(self,request,*args,**kwargs): return self.create(request,*args,**kwargs) #查詢單個數據、刪除數據、修改數據的視圖
class BookView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers def get(self,request,*args,**kwargs): return self.retrieve(request,*args,**kwargs) def put(self,request,*args,**kwargs): return self.update(request,*args,**kwargs) def delete(self,request,*args,**kwargs): return self.destroy(request,*args,**kwargs)

四:restframework視圖組件之使用generices下ListCreateAPIView,RetrieveUpdateDestroyAPIView編寫視圖開發

  views.pyget

from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView class BooksView(ListCreateAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers class BookView(RetrieveUpdateDestroyAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers

五:restframework視圖組件之使用ModelViewSet編寫視圖

  views.py

from rest_framework.viewsets import ModelViewSet class BooksView(ModelViewSet): queryset = models.Book.objects.all() serializer_class = BookSerializers

  urls.py

from app01.views import BooksView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Books/$', BooksView.as_view({"get":"list","post":"create"})), url(r'^Books/(?P<pk>\d+)$', BooksView.as_view({"get":"retrieve","put":"update","delete":"destroy"})), ]
相關文章
相關標籤/搜索