• day100-序列化组件


    3 序列化组件
    
    
    class BookSerializer(serializers.Serializer):
    
        title = serializers.CharField( max_length=32)
        price =serializers.DecimalField(max_digits=5,decimal_places=2)
    
        # 一对多字段
        publish_email=serializers.CharField(max_length=32,source="publish.email")
        publish_name=serializers.CharField(max_length=32,source="publish.name")
    
        # 多对多字段
        # authors=serializers.CharField(max_length=32,source="authors.all")
        authors=serializers.SerializerMethodField()
        def get_authors(self,obj):
            ret=[]
            for i in obj.authors.all():
                ret.append({"name":i.name,"age":i.name})
            return ret
            
    '''
    
    
    serializer=BookSerializer(book_list,many=True)
    serializer.data:
    
    temp=[]
    for obj in book_list:
        temp.append({
                 "title":obj.title,
                 "price":obj.price,
                 "publish":obj.publish.email,
             if 字段SerializerMethodField:
                 "authors":get_authors(obj)
        })
        
        
    json.dumps(temp)
    
    '''
    
    class BookSerializer(serializers.ModelSerializer):
        class Meta:
            model=Book
            # fields=["title","price"]
            fields="__all__"
    
    
    class BookView(APIView):
    
    
    
        def get(self,request):
            book_list=Book.objects.all()
            serializer=BookSerializer(book_list,many=True)
            
            return Response(serializer.data)
            
            
        def post(self,request):
    
            print(request.data)
    
            serializer=BookSerializer(data=request.data,many=False)
    
            if serializer.is_valid():
                serializer.save() # create操作
    
                return Response(serializer.data) # 序列化提交数据
            else:
                return Response(serializer.errors) # 序列化错误信息
    
       
    
    
    今日内容:
     
        1 序列化组件
        
           序列化:    queryset------>[{},{},{}]
                       obj     ------>{}
        
        2 视图组
        
                class AuthorsView(ModelViewSet):
                        queryset = Author.objects.all()
                        serializer_class = AuthorSerializer
     
            
                #url(r'^authors/$', views.AuthorsView.as_view({"get":"list","post":"create"})),
                #url(r'^authors/$', ViewSetMixin.as_view({"get":"list","post":"create"})),
                #url(r'^authors/$', ViewSetMixin.view),
                一旦用户访问,get访问authors/:
                ViewSetMixin.view(reqeust):
                             for method, action in actions.items(): # {"get":"list","post":"create"}
                                    handler = getattr(self, action) #  handler===self.list
                                    setattr(self, method, handler)  #  self.get=self.list
    
                             return self.dispatch(request, *args, **kwargs)    
                             #
                             APIView.dispatch(request):
                                                     
                                                      handler = getattr(self, request.method.lower()) # self.list
                                                      response = handler(request, *args, **kwargs)    
                                                      #     def list(self.request):
               `                                      #            return .....
                                                                
                                                      return response
                                                                
    
    
                ###################
                
                #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
  • 相关阅读:
    个人总结
    再见,我的伪算法竞赛生涯。
    C语言实现迷宫小游戏
    关于第九届蓝桥杯决赛
    蓝桥杯近3年决赛题之3(17年b组)
    蓝桥杯近3年决赛题之二(16年b组)
    蓝桥杯近三年决赛题之一(15年B组)
    第九届蓝桥杯C/C++B组省赛感想
    读书笔记《程序员的自我修养—链接、装载与库》
    蓝桥杯近3年初赛题之三(17年b组)
  • 原文地址:https://www.cnblogs.com/wuchenggong/p/9677551.html
Copyright © 2020-2023  润新知