Django框架之DRF 基于mixins来封装的视图
2019-07-24 09:14:24来源:博客园 阅读 ()
基础视图
示例环境搭建:新建一个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()
原文链接:https://www.cnblogs.com/suguangti/p/11134504.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Django项目中使用qq第三方登录。 2019-08-13
- 使用scrapy框架爬取全书网书籍信息。 2019-08-13
- Django和前端用ajax传输json等数据 2019-08-13
- 网络编程相关知识点 2019-08-13
- Django基本知识 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash