亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

如何正確的使用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

肃宁县| 仪陇县| 施甸县| 沅江市| 罗山县| 永康市| 东丰县| 正定县| 潍坊市| 南江县| 沙河市| 阿拉善左旗| 闽侯县| 镇巴县| 镇康县| 寿阳县| 通城县| 五大连池市| 托里县| 台南县| 东乌| 永寿县| 石棉县| 淄博市| 二连浩特市| 县级市| 恩平市| 濉溪县| 嘉鱼县| 诸城市| 泗水县| 北川| 洛南县| 孝感市| 达州市| 城市| 基隆市| 宁河县| 博客| 扎兰屯市| 青海省|