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

溫馨提示×

溫馨提示×

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

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

Django框架之DRF 基于mixins來封裝的視圖詳解

發布時間:2020-09-18 15:43:13 來源:腳本之家 閱讀:191 作者:suguangti 欄目:開發技術

基礎視圖

示例環境搭建:新建一個Django項目,連接Mysql數據庫,配置路由、視圖函數、序列化單獨創建py文件

# 配置路由

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'^PublishView/', views.PublishView.as_view()),
 url(r'^PublishDetailView/(?P<pk>\d+)', views.PublishDetailView.as_view()),

]
# 視圖:

from django.shortcuts import render
from rest_framework.views import APIView
from app01 import models
from app01.MySer import BookSer,PublishSer,AuthorSer
from rest_framework.response import Response
# Create your views here.
# 基本視圖

class PublishView(APIView):

 def get(self,request):
  publish_list = models.Publish.objects.all()
  bs = PublishSer(publish_list, many=True)
  return Response(bs.data)

 def post(self, request):
  bs = PublishSer(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 = PublishSer(publish_obj, many=False)
  return Response(bs.data)

 def put(self, request, pk):
  publish_obj = models.Publish.objects.filter(pk=pk).first()
  bs = PublishSer(data=request.data, instance=publish_obj)
  if bs.is_valid():
   bs.save()
   return Response(bs.data)
  else:
   return Response(bs.data)

 def delete(self, request, pk):
  models.Publish.objects.filter(pk=pk).delete()
  return Response("")
# MySer.py

from rest_framework import serializers
from app01 import models

class BookSer(serializers.ModelSerializer):
 class Meta:
  model = models.Book
  fields = '__all__'


class PublishSer(serializers.ModelSerializer):
 class Meta:
  model = models.Publish
  fields = '__all__'


class AuthorSer(serializers.ModelSerializer):
 class Meta:
  model = models.Author
  fields = '__all__'

基于mixins來封裝的視圖

其它不變,更改視圖:

# 基于mixins來封裝的視圖
from rest_framework.mixins import CreateModelMixin,\
         ListModelMixin,\
         RetrieveModelMixin,\
         DestroyModelMixin,\
         UpdateModelMixin
from rest_framework.generics import GenericAPIView

class PublishView(CreateModelMixin, ListModelMixin, GenericAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer
 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 PublishDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
 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)

可以看出視圖類中還是有很多冗余代碼

mixins封裝再封裝,第三種方法

# 第三種寫法:
from rest_framework.generics import CreateAPIView,\
         ListCreateAPIView,\
         DestroyAPIView,\
         RetrieveUpdateDestroyAPIView

class PublishView(ListCreateAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

class PublishDetailView(RetrieveUpdateDestroyAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

還是有冗余代碼

第四種寫法,再次封裝,全部寫在一個類中

# 路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
 url(r'^admin/', admin.site.urls),

 url(r'^publish/$', views.PublishView.as_view({'get': 'list', 'post': 'create'})),
 url(r'^publish/(?P<pk>\d+)$', views.PublishView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),

]
# 第四種寫法:5個接口寫在一個類中
from rest_framework.viewsets import ModelViewSet
class PublishView(ModelViewSet):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

補充:

from rest_framework.viewsets import ViewSetMixin
from rest_framework.views import APIView
# ViewSetMixin 重寫了as_view方法
class Test(ViewSetMixin,APIView):

 def aaa(self,request):
  return Response()

以上這篇Django框架之DRF 基于mixins來封裝的視圖詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

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

AI

天津市| 揭东县| 车致| 巴东县| 兰坪| 鱼台县| 神池县| 大庆市| 温州市| 乐业县| 漾濞| 托克逊县| 屏东市| 新营市| 景洪市| 六盘水市| 安陆市| 都江堰市| 宣化县| 平武县| 灌南县| 方城县| 精河县| 都江堰市| 木兰县| 巴马| 平邑县| 平顶山市| 尚义县| 湖南省| 左贡县| 行唐县| 丰县| 集贤县| 沙坪坝区| 清苑县| 信阳市| 建阳市| 平度市| 潜山县| 天津市|