中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

泽普县| 潼关县| 崇州市| 历史| 浮梁县| 肥乡县| 泸州市| 灵山县| 盈江县| 织金县| 宣城市| 奎屯市| 泌阳县| 太仓市| 昌都县| 磐石市| 定西市| 富阳市| 鹰潭市| 永仁县| 福贡县| 临湘市| 勐海县| 元阳县| 伽师县| 东方市| 灵寿县| 栾城县| 朝阳县| 闸北区| 沙河市| 秦安县| 丰都县| 信丰县| 若羌县| 静宁县| 砀山县| 迁西县| 高尔夫| SHOW| 阿坝县|