Django框架之DRF 基于mixins来封装的视图
生活随笔
收集整理的這篇文章主要介紹了
Django框架之DRF 基于mixins来封装的视图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
閱讀目錄
- 基礎視圖
- 基于mixins來封裝的視圖
- mixins封裝再封裝,第三種方法
- 第四種寫法,再次封裝,全部寫在一個類中
基礎視圖
示例環境搭建:新建一個Django項目,連接Mysql數據庫,配置路由、視圖函數、序列化單獨創建py文件
-------------------------------------------------------------------- 注:如果你對python感興趣,我這有個學習Python基地,里面有很多學習資料,感興趣的+Q群:895817687 --------------------------------------------------------------------# 配置路由from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [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.pyfrom rest_framework import serializers from app01 import modelsclass BookSer(serializers.ModelSerializer):class Meta:model = models.Bookfields = '__all__'class PublishSer(serializers.ModelSerializer):class Meta:model = models.Publishfields = '__all__'class AuthorSer(serializers.ModelSerializer):class Meta:model = models.Authorfields = '__all__'基于mixins來封裝的視圖
其它不變,更改視圖:
# 基于mixins來封裝的視圖 from rest_framework.mixins import CreateModelMixin,\ListModelMixin,\RetrieveModelMixin,\DestroyModelMixin,\UpdateModelMixin from rest_framework.generics import GenericAPIViewclass PublishView(CreateModelMixin, ListModelMixin, GenericAPIView):queryset = models.Publish.objects.all()serializer_class = PublishSerdef 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 = PublishSerdef 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,\RetrieveUpdateDestroyAPIViewclass PublishView(ListCreateAPIView):queryset = models.Publish.objects.all()serializer_class = PublishSerclass PublishDetailView(RetrieveUpdateDestroyAPIView):queryset = models.Publish.objects.all()serializer_class = PublishSer還是有冗余代碼
回到頂部
第四種寫法,再次封裝,全部寫在一個類中
# 路由 from django.conf.urls import url from django.contrib import admin from app01 import viewsurlpatterns = [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来封装的视图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django框架之DRF get pos
- 下一篇: Django框架深入了解_04(DRF之