博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REST_FRAMEWORK加深记忆-三种CLASS VIEW的进化史
阅读量:5889 次
发布时间:2019-06-19

本文共 2897 字,大约阅读时间需要 9 分钟。

一层一层的封装,又能到底层,就会有全局感啦。。。

from rest_framework import statusfrom rest_framework.response import Responsefrom snippets.models import Snippetfrom snippets.serializers import SnippetSerializerfrom django.http import Http404from rest_framework.views import APIViewfrom rest_framework import mixinsfrom rest_framework import genericsclass SnippetListOld(APIView):    def get(self, request, format=None):        snippets = Snippet.objects.all()        serializer = SnippetSerializer(snippets, many=True)        return Response(serializer.data)    def post(self, request, format=None):        serializer = SnippetSerializer(data=request.data)        if serializer.is_valid():            serializer.save()            return Response(serializer.data, status=status.HTTP_201_CREATED)        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)class SnippetDailOld(APIView):    def get_object(self, pk):        try:            return Snippet.object.get(pk=pk)        except Snippet.DoesNotExist:            raise Http404    def get(self, request, pk, format=None):        snippet = self.get_object(pk)        serializer = SnippetSerializer(snippet)        return Response(serializer.data)    def put(self, request, pk, format=None):        snippet = self.get_object(pk)        serializer = SnippetSerializer(snippet, data=request.data)        if serializer.is_valid():            serializer.save()            return Response(serializer.data)        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)    def delete(self, request, pk, format=None):        snippet = self.get_object(pk)        snippet.delete()        return Response(status=status.HTTP_204_NO_CONTENT)class SnippetListOld2(mixins.ListModelMixin,                  mixins.CreateModelMixin,                  generics.GenericAPIView):    queryset = Snippet.objects.all()    serializer_class = SnippetSerializer    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 SnippetDetailOld2(mixins.RetrieveModelMixin,                    mixins.UpdateModelMixin,                    mixins.DestroyModelMixin,                    generics.GenericAPIView):    queryset = Snippet.objects.all()    serializer_class = SnippetSerializer    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)class SnippetList(generics.ListCreateAPIView):    queryset = Snippet.objects.all()    serializer_class = SnippetSerializerclass SnippetDetail(generics.RetrieveUpdateDestroyAPIView):    queryset = Snippet.objects.all()    serializer_class = SnippetSerializer

 

转载地址:http://oqysx.baihongyu.com/

你可能感兴趣的文章
高级Bash脚本编程指南(27):文本处理命令(三)
查看>>
JavaScript---事件
查看>>
Android NDK入门实例 计算斐波那契数列一生成jni头文件
查看>>
c/c++性能优化--I/O优化(上)
查看>>
将HTML特殊转义为实体字符的两种实现方式
查看>>
jquery 保留两个小数的方法
查看>>
网站架构设计的误区
查看>>
Standard C++ Programming: Virtual Functions and Inlining
查看>>
html5 Web Workers
查看>>
iis 故障导致网站无法访问
查看>>
作业抄袭简单检测
查看>>
ASP.NET 回调技术(CallBack)
查看>>
Spark源码分析 – BlockManager
查看>>
JS中的this
查看>>
人生, 不要在别扭的事上纠结
查看>>
C的面向对象编程
查看>>
日志服务器架构设计
查看>>
使用Unity开发Android的几种调试方法
查看>>
C++ 基础笔记(一)
查看>>
编译内核出错:invalid option `abi=aapcs-linux' 解决办法
查看>>