zoukankan      html  css  js  c++  java
  • 序列化组件

    from rest_framework import serializers
    
    
    class AuthorSerializer(serializers.Serializer):
        nid = serializers.CharField()
        name = serializers.CharField()
        age = serializers.CharField()
    
    
    # class BookSerializer(serializers.Serializer):
    #     # 可以用source修改别名
    #     xx = serializers.CharField(source='name')
    #     price = serializers.CharField()
    #     sb = serializers.CharField(source='publish.city')
    #     test = serializers.CharField(source='publish.test')
    #     publish_id = serializers.CharField()
    #     # SerializerMethodField,可以指定一个方法
    #     # publish=serializers.SerializerMethodField()
    #     # # 方法名:叫get_字段名,要传参数,参数是:当前book对象
    #     # def get_publish(self,obj):
    #     #     # obj 是当前book对象0
    #     #     dic={'name':obj.publish.name,'email':obj.publish.email}
    #     #     return dic
    #     authors=serializers.SerializerMethodField()
    #     # def get_authors(self,book):
    #     #     #拿到这本书的所有作者
    #     #     aus=book.authors.all()
    #     #     # 拼出一个列表,列表中套字典,每个字典是一个作者
    #     #     ll=[]
    #     #     for a in aus:
    #     #         ll.append({'name':a.name,'age':a.age})
    #     #     return ll
    #     def get_authors(self,book):
    #         #拿到这本书的所有作者
    #         aus=book.authors.all()
    #         # 可以继续用序列化类,来处理
    #         auth_ser=AuthorSerializer(aus,many=True)
    #         return auth_ser.data
    from ap01.models import Book
    
    
    # class BookSerializer(serializers.ModelSerializer):
    #     # 必须写一个内部类,名字叫Meta
    #     class Meta:
    #         model = Book
    #         fields = '__all__'
    #     # 重写属性
    #     publish = serializers.CharField(source='publish.name')
    #     authors = serializers.SerializerMethodField()
    #     def get_authors(self, book):
    #         # 拿到这本书的所有作者
    #         aus = book.authors.all()
    #         # 可以继续用序列化类,来处理
    #         auth_ser = AuthorSerializer(aus, many=True)
    #         return auth_ser.data
    
    
    class BookSerializer(serializers.ModelSerializer):
        # 必须写一个内部类,名字叫Meta
        class Meta:
            model = Book
            # fields = '__all__'
            # 指定只取这两个字段
            fields = ['nid','name']
            # 去掉指定的字段
            # exclude=['publish','authors']
            # fields,跟exclude不能连用
            # 指定深度(官方建议小于10,我给你的建议小于3)
            # depth = 2
    mySer.py
    from django.shortcuts import render
    from ap01 import models
    # Create your views here.
    from rest_framework.views import  APIView
    from ap01.mySer import  BookSerializer
    from django.http import JsonResponse
    class Books(APIView):
        def get(self,request,*args,**kwargs):
    
            ret=models.Book.objects.all()
            #生成一个序列化的对象,传参数
            #序列化多条,记住many=True
            book_ser=BookSerializer(ret,many=True)
            print(book_ser.data)
            return JsonResponse(book_ser.data,safe=False)
    views.py
  • 相关阅读:
    关于AutoResetEvent和ManualResetEvent
    (转)使用 ODP.NET 和引用游标优化结果集
    胰腺
    SQL Cache Dependency
    败犬的远吠?
    吃亏和付出经常是必须的(转)
    AutoResetEvent 与 ManualResetEvent送花例子
    linux yum install
    SpringMVC+JPA+SpringData配置
    Spring AOP 实现原理
  • 原文地址:https://www.cnblogs.com/xuqidong/p/13709383.html
Copyright © 2011-2022 走看看