摘要:將資源連接到視圖和的約定可以使用類自動處理。視圖視圖集之間的權衡使用視圖集可以是一個非常有用的抽象。這并不意味著采用視圖集總是正確的方法。在使用基于類的視圖而不是基于函數的視圖時,有一個類似的權衡要考慮。
視圖集 簡介
REST框架包括一個用于處理ViewSets的抽象,它允許開發人員集中精力對API的狀態和交互進行建模,并根據常規約定自動處理URL構造。
ViewSet類與View類幾乎相同,不同之處在于它們提供諸如read或update之類的操作,而不是get或put等方法處理程序
ViewSet類只綁定到一組方法處理程序,當它被實例化成一組視圖的時候,通常通過使用一個Router類來處理自己定義URL conf的復雜性。
路由器因為我們使用的是ViewSet類而不是View類,我們實際上不需要自己設計URL。將資源連接到視圖和url的約定可以使用Router類自動處理。我們需要做的就是使用路由器注冊相應的視圖集,然后讓它執行其余操作。視圖vs視圖集之間的權衡
使用視圖集可以是一個非常有用的抽象。它有助于確保URL約定在你的API中保持一致,最大限度地減少編寫所需的代碼量,讓你能夠專注于API提供的交互和表示,而不是URLconf的細節。
這并不意味著采用視圖集總是正確的方法。在使用基于類的視圖而不是基于函數的視圖時,有一個類似的權衡要考慮。使用視圖集不像多帶帶構建視圖那樣明確。
不同的版本演練 版本一idc/urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r"idcs/$",views.idc_list), url(r"idcs/(?P[0-9]+)/$",views.idc_detail), ]
models.py
from django.db import models # Create your models here. class Idc(models.Model): name = models.CharField("IDC名稱",max_length=128,blank=False,null=True) address = models.CharField("IDC地址",max_length=200,default="") phone = models.CharField("IDC聯系電話",max_length=20,null=True) email = models.EmailField("IDC郵箱") def __str__(self): return self.name class Meta: db_table = "idc"
serializers.py
from rest_framework import serializers from .models import Idc class IdcSerializer(serializers.Serializer): id = serializers.IntegerField(read_only=True) name = serializers.CharField(required=False) address = serializers.CharField(required=False) phone = serializers.CharField(required=False) email = serializers.EmailField(required=False) def update(self, instance, validated_data): instance.name = validated_data.get("name",instance.name) instance.address = validated_data.get("address",instance.address) instance.phone = validated_data.get("phone",instance.phone) instance.email = validated_data.get("email",instance.email) instance.save() return instance def create(self, validated_data): return Idc.objects.create(**validated_data)
views.py
from .models import Idc from .serializers import IdcSerializer from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from django.http import HttpResponse,JsonResponse class JSONResponse(HttpResponse): def __init__(self,data,**kwargs): kwargs.setdefault("content_type", "application/json") content = JSONRenderer().render(data) super(JSONResponse,self).__init__(content=content,**kwargs) def idc_list(request,*args,**kwargs): if request.method == "GET": queryset = Idc.objects.all() serializer = IdcSerializer(queryset,many=True) return JSONResponse(serializer.data) # content = JSONRenderer().render(serializer.data) # return HttpResponse(content,content_type="application/json") elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() content = JSONRenderer().render(serializer.data) return HttpResponse(content, content_type="application/json") def idc_detail(request,pk,*args,**kwargs): try: idc = Idc.objects.get(pk=pk) except Idc.DoesNotExist: return HttpResponse(status=404) if request.method == "GET": serializer = IdcSerializer(idc) return JSONResponse(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) elif request.method == "PUT": content = JSONParser().parse(request) serializer = IdcSerializer(idc,data=content) #這里要注意傳入instance(idc)否則就是創建操作 if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) elif request.method == "DELETE": idc.delete() return HttpResponse(status=204)版本二
基于rest_framework下的:Response方法,status定義和api_view裝飾器
idc/urls.py
## 版本二 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.idc_list_v2,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.idc_detail_v2,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
idc/views.py
### 版本二 from rest_framework.response import Response from rest_framework import status from rest_framework.decorators import api_view @api_view(["GET","POST"]) def idc_list_v2(request,*args,**kwargs): if request.method == "GET": queryset = Idc.objects.all() serializer = IdcSerializer(queryset,many=True) return Response(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data,status=status.HTTP_201_CREATED) return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST) @api_view(["GET","POST","PUT","DELETE"]) def idc_detail_v2(request,pk,*args,**kwargs): try: idc = Idc.objects.get(pk=pk) except Idc.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "GET": serializer = IdcSerializer(idc) return Response(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data) elif request.method == "PUT": content = JSONParser().parse(request) serializer = IdcSerializer(idc,data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data) elif request.method == "DELETE": idc.delete() return Response(status=status.HTTP_204_NO_CONTENT) from rest_framework.reverse import reverse @api_view(["GET"]) def api_root(request,format=None,*args,**kwargs): return Response({ "idcs":reverse("idc-list",request=request,format=format) })
測試:
瀏覽器打開:http://192.168.33.10:8080/idcs/ 即可
版本三基于類視圖的版本,要注意接受提交數據用的是request.data
idc/urls.py
###版本三 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcListView.as_view,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetailView.as_view,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
idc/views.py
### 版本三 from rest_framework.views import APIView from django.http import Http404 class IdcListView(APIView): def get(self,request,format=None): queryset = Idc.objects.all() serializer = IdcSerializer(queryset, many=True) return Response(serializer.data) def post(self,request,format=None): #content = JSONParser().parse(request) serializer = IdcSerializer(data=request.data) #request.data獲取請求參數 if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST) class IdcDetailView(APIView): def get_obj(self,pk): try: return Idc.objects.get(pk=pk) except Idc.DoesNotExist: raise Http404 def get(self,request,pk,format=None): idc = self.get_obj(pk) serializer = IdcSerializer(idc) return Response(serializer.data) def put(self,request,pk,format=None): idc = self.get_obj(pk) serializer = IdcSerializer(idc,data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST) def delete(self,request,pk,format=None): idc = self.get_obj(pk) idc.delete() return Response(status=status.HTTP_204_NO_CONTENT)版本四
idc/urls.py
###版本四 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcList_V4.as_view(),name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetail_V4.as_view(),name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
###版本四 from rest_framework import mixins,generics class IdcList_V4(generics.GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer 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 IdcDetail_V4(generics.GenericAPIView, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer 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)版本五
urls.py
###版本五 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcList_V5.as_view(),name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetail_V5.as_view(),name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
### 版本五 class IdcList_V5(generics.ListCreateAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer class IdcDetail_V5(generics.RetrieveUpdateDestroyAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer版本六
urls.py
## 版本六 idc_list = views.IdcViewSet.as_view({ "get":"list", "post":"create" }) idc_detail = views.IdcViewSet.as_view({ "get":"retrieve", "put":"update", "delete":"destroy" }) urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",idc_list,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",idc_detail,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
####版本六 from rest_framework import viewsets class IdcViewSet(viewsets.GenericViewSet, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, mixins.CreateModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer版本七
urls.py
###版本七 from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register("idcs",views.IdcViewSet_V7) #自動適配,字啊需要加的話就直接加 urlpatterns = [ url(r"^",include(router.urls)) ]
views.py
### 版本七 class IdcViewSet_V7(viewsets.ModelViewSet): queryset = Idc.objects.all() serializer_class = IdcSerializer
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/42740.html
摘要:為什么要進行前后端分離多端適配開發模式的流行前后端職責不清前后端開發效率問題,前后端相互等待前段一直配合后端,能力受到限制后端開發語言和模板高度耦合,導致開發語言依賴嚴重前后端分離的缺點前后端學習門檻增加數據依賴,導致文檔的重要性增加前段工 DRF 為什么要進行前后端分離 PC,APP,PAD多端適配 SPA開發模式的流行 前后端職責不清 前后端開發效率問題,前后端相互等待 前段一直...
摘要:實戰小練習需求獲取指定組下的列表往用戶組添加用戶從組中刪除用戶需求分析獲取指定組下的列表網用戶組添加用戶從組中刪除用戶走界面不行項目結構主路由配置文件開源運維平臺子路由配置文件序列 實戰小練習 需求 users userGroups groups groupUsers---- get : 獲取指定組下的user列表-----put:往用戶組添加用戶-----delete:從組中刪除用...
摘要:后端實現跨域第三方擴展在介紹之前,我先介紹兩個概念同源策略跨域同源策略同源策略是一種約定,是瀏覽器的一種安全機制。這里同源需要協議域名端口三者都相同,否則不能進行訪問。跨域不同源之間的網站通信就是跨域。 在使用django-rest-framework開發項目的時候我們總是避免不了跨域的問題,因為現在大多數的項目都是前后端分離,前后端項目部署在不同的web服務器上,因為我們是后端程序員...
閱讀 1858·2021-11-25 09:43
閱讀 3692·2021-11-24 10:32
閱讀 1083·2021-10-13 09:39
閱讀 2337·2021-09-10 11:24
閱讀 3351·2021-07-25 21:37
閱讀 3470·2019-08-30 15:56
閱讀 864·2019-08-30 15:44
閱讀 1454·2019-08-30 13:18