基础视图
示例环境搭建:新建一个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/("htmlcode"># 视图: 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/("htmlcode"># 第四种写法: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来封装的视图详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]