zoukankan      html  css  js  c++  java
  • AI-视图组件-五个接口的最终简化版

    五个接口最终版

    #url.py
    # 序列化最贱版本
        url(r'^customer/$', views.CustomerView.as_view({"get":"list","post":"create"})),
        url(r'^customer/(?P<pk>d+)', views.CustomerView.as_view({"get":"retrieve","put":"update","delete":"destroy"})),
    views.py
    from rest_framework.serializers import ModelSerializer
    class CustomerSerializer(serializers.ModelSerializer):
        class Meta:
            model=Customer
            # field=["name","gender"]
            fields="__all__"
    
    from rest_framework.viewsets import ModelViewSet
    class CustomerView(ModelViewSet): #将五个数据接口全部封装在了ModelViewSet
        queryset = Customer.objects.all()
        serializer_class = CustomerSerializer

    五个接口最终版源码解析

    #url(r'^authors/(?P<pk>d+)/', views.AuthorsView.as_view({"get":"retrieve","delete":"destroy","put":"update"})),
                #url(r'^authors/$', ViewSetMixin.as_view({"get":"retrieve","delete":"destroy","put":"update"})),
                #url(r'^authors/$', ViewSetMixin.view),
                一旦用户访问,get访问authors/1:
                ViewSetMixin.view(reqeust):
                             for method, action in actions.items(): # {"get":"retrieve","delete":"destroy","put":"update"}
                                    handler = getattr(self, action) #  handler===self.retrieve    handler=== destroy    handler===update
                                    setattr(self, method, handler)  #  self.get=self.retrieve    self.delete=self.destroy   self.put=self.update
    
                             return self.dispatch(request, *args, **kwargs)    
                             #
                             APIView.dispatch(request):
                                                     
                                                      handler = getattr(self, request.method.lower()) # self.retrieve
                                                      response = handler(request, *args, **kwargs)    
                                                      #     def retrieve(self.request):
               `                                      #            return .....
                                                                
                                                      return response
  • 相关阅读:
    spring-boot4
    spring-boot3代码
    spring-boot3
    spring-boot2代码
    spring-boot2
    Java Socket编程
    eclipse项目中.classpath文件详解
    spring-boot1
    ASCII 在线转换器
    如何在Android开发中让你的代码更有效率
  • 原文地址:https://www.cnblogs.com/djfboai/p/10072732.html
Copyright © 2011-2022 走看看