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

溫馨提示×

溫馨提示×

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

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

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

孟州市| 桂东县| 于田县| 石城县| 梧州市| 安义县| 海原县| 舒城县| 山东| 深圳市| 灌云县| 贵德县| 吉安市| 甘谷县| 宣恩县| 永昌县| 杨浦区| 临安市| 云和县| 西平县| 霍邱县| 祁门县| 锡林浩特市| 富锦市| 鱼台县| 万年县| 灵宝市| 乌海市| 陵川县| 施甸县| 商水县| 石柱| 若尔盖县| 信阳市| 任丘市| 清新县| 昌黎县| 长武县| 仪征市| 澄迈县| 武邑县|