您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何正確的使用Restful_framework視圖組件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一、基本視圖
寫一個出版社的增刪查改resful接口
要自己事先創建好django項目,并創建好表,添加完記錄
路由:
url(r'^publish/$', views.PublishView.as_view()),
url(r'^publish/(?P<pk>\d+)/$', views.PublishDetailView.as_view()),
視圖:
from api import modelsfrom rest_framework import serializersfrom rest_framework.response import Responsefrom rest_framework.views import APIView class PublishSerializers(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class PublishView(APIView): #查詢出版社表中所有記錄 def get(self, request): publish_list = models.Publish.objects.all() bs = PublishSerializers(publish_list, many=True) # 序列化數據 return Response(bs.data) #返回的結果是列表中套字典 def post(self, request): # 添加一條數據 print(request.data) bs=PublishSerializers(data=request.data) if bs.is_valid(): bs.save() # 生成記錄 return Response(bs.data) #返回的是添加成功記錄的字典 else: return Response(bs.errors) class PublishDetailView(APIView): #查詢某一條數據 def get(self,request,pk): publish_obj=models.Publish.objects.filter(pk=pk).first() bs=PublishSerializers(publish_obj,many=False) return Response(bs.data) #更新某一條數據 def put(self,request,pk): publish_obj = models.Publish.objects.filter(pk=pk).first() bs=PublishSerializers(data=request.data,instance=publish_obj) if bs.is_valid(): bs.save() # update return Response(bs.data) else: return Response(bs.errors) #刪除某一條數據 def delete(self,request,pk): models.Publish.objects.filter(pk=pk).delete() return Response("")
二、mixins類和generic類編寫視圖
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/(?P<pk>\d+)', views.PublishDetail.as_view()),
視圖:
from api import models from rest_framework import serializers from rest_framework import mixins from rest_framework.generics import GenericAPIView class PublishSer(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer def get(self,request): return self.list(request) #在ListModelMixin下有一個list方法,返回的就是查詢的結果:Response(serializer.data) def post(self,request): return self.create(request) #已經封裝好了,幫我們做校驗和序列化,返回的是添加成功的記錄 class PublishDetail(GenericAPIView,mixins.DestroyModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer 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)
三、使用generic下ListCreateAPIView和RetrieveUpdateDestroyAPIView
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/(?P<pk>\d+)', views.PublishDetail.as_view()),
視圖:
from api import models from rest_framework import mixins from rest_framework.generics import GenericAPIView from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView from rest_framework import serializers class Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(ListCreateAPIView): queryset = models.Publish.objects.all() serializer_class = Publishser class PublishDetail(RetrieveUpdateDestroyAPIView): queryset = models.Publish.objects.all() serializer_class = Publishser
四、使用ModelViewSet
路由:
url(r'^publish$',views.Publish.as_view({'get':'list','post':'create'})),
url(r'^publish/(?P<pk>\d+)', views.Publish.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
視圖:
from rest_framework.viewsets import ModelViewSet from api import models from rest_framework import serializers class Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(ModelViewSet): queryset = models.Publish.objects.all() serializer_class = Publishser
關于如何正確的使用Restful_framework視圖組件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。