您好,登錄后才能下訂單哦!
基礎視圖
示例環境搭建:新建一個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來封裝的視圖詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。