91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何正確的使用Restful_framework視圖組件

發布時間:2020-11-18 14:36:23 來源:億速云 閱讀:150 作者:Leah 欄目:開發技術

這篇文章給大家介紹如何正確的使用Restful_framework視圖組件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一、基本視圖

寫一個出版社的增刪查改resful接口

要自己事先創建好django項目,并創建好表,添加完記錄

路由:

url(r'^publish/$', views.PublishView.as_view()),
url(r'^publish/(&#63;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/(&#63;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/(&#63;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/(&#63;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視圖組件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

古田县| 五原县| 仁布县| 伊春市| 昌乐县| 稻城县| 安仁县| 磴口县| 息烽县| 高碑店市| 广平县| 深泽县| 牟定县| 塔河县| 堆龙德庆县| 抚顺县| 仲巴县| 安西县| 金华市| 辉县市| 陕西省| 曲松县| 漾濞| 温泉县| 报价| 娱乐| 墨玉县| 定边县| 威远县| 金门县| 鞍山市| 荃湾区| 永善县| 泗水县| 句容市| 疏附县| 来宾市| 天气| 宜阳县| 托里县| 双鸭山市|