REST : rest_framework.decorators.api_view 實現PATCH
對 做補充
點選(此處)摺疊或開啟
-
@api_view(['GET', 'PATCH', 'DELETE'])
-
def snippet_detail(request, pk):
-
"""
-
Retrieve, update or delete a snippet instance.
-
"""
-
try:
-
snippet = Snippet.objects.get(pk=pk)
-
except Snippet.DoesNotExist:
-
return Response(status=status.HTTP_404_NOT_FOUND)
-
-
elif request.method == 'PATCH':
-
newData = transferPatchData(snippet, request.data)
-
serializer = SnippetSerializer(snippet, data=newData)
-
if serializer.is_valid():
-
serializer.save()
-
return Response(serializer.data)
-
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
-
-
def transferPatchData(obj, reqtData):
-
newData = {}
-
for attrName in obj.__dict__.keys():
-
if re.search(r"^_", attrName):
-
continue
-
if reqtData.has_key(attrName):
-
newData[attrName] = reqtData.get(attrName)
-
else:
-
newData[attrName] = getattr(obj, attrName)
- return newData
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2120779/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- REST:使用PATCH進行部分更新 - mscharhagREST
- CompletableFuture中實現多個 REST 呼叫REST
- IOS熱更新-JSPatch實現原理+Patch現場恢復iOSJS
- 使用動態規劃 實現字元級Diff & Patch動態規劃字元
- 在 ASP .NET Core 中實現冪等 REST APIRESTAPI
- 用ASP.NET Core 2.0 建立規範的 REST API -- DELETE, UPDATE, PATCH 和 LogASP.NETRESTAPIdelete
- SpringMVC聯手REST實現入門級的CRUDSpringMVCREST
- git 生成patch和使用patchGit
- REST開發最佳實踐REST
- Rest Assured+TestNg實現資料驅動的介面測試REST
- 如何使用git 生成patch 和打入patchGit
- REST實戰討論組FAQREST
- Segment使用Go、gRPC和Envoy作為後端REST API實現GoRPC後端RESTAPI
- 使用 Spring Security JWT 令牌簽名實現 REST API 安全性SpringJWTRESTAPI
- Diff and Patch
- 【Rest】PUT Vs Post in RestREST
- LINUX 的patch 製作,及打patchLinux
- 打 patch 報錯:corrupt patch at line 36
- 基於libcurl實現REST風格http/https的get和postRESTHTTP
- Vue結合Django-Rest-Frameword結合實現登入認證(一)VueDjangoREST
- Vue結合Django-Rest-Frameword結合實現登入認證(二)VueDjangoREST
- REST is not enabled. use -rest to turn onREST
- aix打patchAI
- OAF and Jdev Patchdev
- 如何使用Spring Boot,Spring Data和H2 DB實現REST APISpring BootRESTAPI
- Web Service實踐之REST vs RPCWebRESTRPC
- [譯] 將現有的 API 從 REST 遷移到 GraphQLAPIREST
- REST APIsRESTAPI
- rest apiRESTAPI
- The REST ObjectionRESTObject
- REST真相REST
- 2.3.3.4 Application PatchAPP
- oracle安裝patchOracle
- Oracle ebs patch typeOracle
- 13 個設計 REST API 的最佳實踐RESTAPI
- 建立REST SOE實戰演練系列連結REST
- 到底什麼樣的 REST 才是最佳 REST?REST
- SOA之(5)——REST的SOA(SOA with REST)概念REST