zoukankan      html  css  js  c++  java
  • Restful framework【第四篇】视图组件

    基本使用

    -view的封装过程有空可以看一下
    -ViewSetMixin
    	# ViewSetMixin 写在前面,先找ViewSetMixin的as_view方法
    	# 用了ViewSetMixin ,视图类中,不需要再写get,post,delete....这些函数了,函数名可以自定义
    	# 而且这个视图类,可以响应多条路由
    		
      -使用:
    	-urls.py中
    		url(r'^publishs/', views.PublishView.as_view({'get': 'aaa','post':'ddd'})),
    		url(r'^bbb/', views.PublishView.as_view({'get': 'bbb','post':'ccc'})),
    				
    	-视图类中:
    		class PublishView(ViewSetMixin,APIView):   #注意这里类的顺序,ViewSetMixin写在前面
    			def aaa(self,request):
    				return Response({'status':100})
    			def bbb(self,request):
    				return Response({'bb': "bbb"})

    基本视图

    写一个出版社的增删查改resful接口

    路由:

    url(r'^publish/$', views.PublishView.as_view()),
    url(r'^publish/(?P<pk>d+)/$', views.PublishDetailView.as_view()),

    视图

    class PublishSerializers(serializers.ModelSerializer):
        class Meta:
            model=models.Publish
            fields='__all__'
    
    class PublishView(APIView):
    
        def get(self, request):
            publish_list = models.Publish.objects.all()
            bs = PublishSerializers(publish_list, many=True)
            # 序列化数据
    
            return Response(bs.data)
    
        def post(self, request):
            # 添加一条数据
            print(request.data)
    
            bs=PublishSerializers(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=PublishSerializers(publish_obj,many=False)
            return Response(bs.data)
        def put(self,request,pk):
            publish_obj = models.Publish.objects.filter(pk=pk).first()
    
            bs=PublishSerializers(data=request.data,instance=publish_obj)
            if bs.is_valid():
                bs.save() # update
                return Response(bs.data)
            else:
                return Response(bs.errors)
        def delete(self,request,pk):
            models.Publish.objects.filter(pk=pk).delete()
    
            return Response("")

    mixin类和generice类编写视图

    from rest_framework.mixins import CreateModelMixin,RetrieveModelMixin,ListModelMixin,UpdateModelMixin,DestroyModelMixin
    from rest_framework.generics import GenericAPIView
    class PublishView(ListModelMixin,CreateModelMixin,GenericAPIView): queryset=models.Publish.objects.all() serializer_class=PublishSerializers def get(self, request): return self.list(request) def post(self, request): return self.create(request) class PublishDetailView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView): queryset=models.Publish.objects.all() serializer_class=PublishSerializers
    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)

    使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView

    from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
    class PublishView(ListCreateAPIView):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers
    
    class PublishDetailView(RetrieveUpdateDestroyAPIView):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers

    使用ModelViewSet

    路由:

    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'})),

    视图:

    from rest_framework.viewsets import ModelViewSet
    class PublishView(ModelViewSet):
        queryset=models.Publish.objects.all()
        serializer_class=PublishSerializers
  • 相关阅读:
    无阻塞网络
    带宽、线速、吞吐量
    one-to-all及all-to-all网络通信模式
    CLOS网络架构与FATTREE胖树拓扑
    CLOS网络
    IP分片与重组详解
    原 TCP层的分段和IP层的分片之间的关系 & MTU和MSS之间的关系
    多个方面比较电路交换、报文交换和分组交换的主要优缺点
    地址族与数据序列 (转)
    简单网络搭建与测试 mininet
  • 原文地址:https://www.cnblogs.com/596014054-yangdongsheng/p/10228810.html
Copyright © 2011-2022 走看看