• Serializers序列化组件


    Django的序列化方法

      .values 序列化结果

    class BooksView(View):
        def get(self, request):
            book_list = Book.objects.values('id', 'title', 'chapter', 'pub_time', 'publisher')
            book_list = list(book_list)
            # 如果我们需要取外键关联的字段信息,需要循环获取外键,再去数据库查然后拼接成我们想要的
            ret = []
            for book in book_list:
                pub_dict = {}
                pub_obj = Publish.objects.filter(pk=book['publisher']).first()
                pub_dict['id'] = pub_obj.pk
                pub_dict['title'] = pub_obj.title
                book['publisher'] = pub_dict
                ret.append(book)
            ret = json.dumps(book_list, ensure_ascii=False, cls=MyJson)
            # json不能解决时间格式的数据
            return HttpResponse(ret)
    
    
    
    # json.JSONEncoder.default()
    # 解决json不能序列化时间字段的问题
    class MyJson(json.JSONEncoder):
        def default(self, field):
            if isinstance(field, datetime.datetime):
                return field.strftime('%Y-%m-%d %H:%M:%S')
            elif isinstance(field, datetime.date):
                return field.strftime('%Y-%m-%d')
            else:
                return json.JSONEncoder.default(self, field)
    

      django serializers

    from django.core import serializers
    
    # 能够得到我们要的效果,结构有点复杂
    class BooksView(View):
        def get(self, request):
            book_list = Book.objects.all()
            ret = serializers.serialize('json', book_list)
            return HttpResponse(ret)
    

    DRF序列化的方法

    首先,我们要用DRF的序列化,就要遵循人家框架的一些标准,

      -- Django我们CBV继承类是View,现在DRF我们要用APIView

      -- Django中返回的时候我们用HTTPResponse,JsonResponse,render,DRF我们用Response

    序列化

      第一步,声明序列化类

    // 定义一个序列化器类
    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField()
        title = serializers.CharField(max_length=32)
        CHOICES = ((1, 'Linux'),(2,'Django'),(3,'Python'))
        chapter = serializers.ChoiceField(choices=CHOICES,
            source='get_chapter_display')
        pub_time = serializers.DateField() 

      第二步,序列化对象

    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    
    class BookView(APIView):
        def get(self, request):
            book_list = Book.objects.all()
            # 序列化对象,many=True为可以序列化多个
            ret = BookSerializer(book_list, many=True)
            # 序列化的数据存放在data中
            return Response(ret.data) 

    外键关系的序列化

      外键关系的序列化

    from rest_framework import serializers
    from .models import Book
      

    class PublisherSerializer(serializers.Serializer):
      id = serializers.IntegerField(read_only=True)
      title = serializers.CharField(max_length=32)

    class UserSerializer(serializers.Serializer):
      id = serializers.IntegerField(read_only=True)
      name = serializers.CharField(max_length=32)
      age = serializers.IntegerField()

    class BookSerializer(serializers.Serializer):
      id = serializers.IntegerField(read_only=True)
      title = serializers.CharField(max_length=32)
      CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
      category= serializers.ChoiceField(choices=CHOICES,
      source='get_category_display',read_only=True)
      pub_time = serializers.DateField()

      publisher = PublisherSerializer(read_only=True)
      user = UserSerializer(many=True,read_only=True)

    反序列化

    当前端给我们发post的请求的时候~前端给我们传过来的数据~我们要进行一些校验然后保存到数据库~

    首先~我们要写反序列化用的一些字段~有些字段要跟序列化区分开~~

    Serializer提供了.is_vaild()和.save()方法~~

      反序列化serializer.py

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
      title = serializers.CharField(max_length=32)
      CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
      category = serializers.ChoiceField(choices=CHOICES,
        source='get_category_display',read_only=True)
      post_category = serializers.IntegerField(write_only=True)
      pub_time = serializers.DateField()
      
      publisher = PublisherSerializer(read_only=True)
      user = UserSerializer(many=True, read_only=True)

      users = serializers.ListField(write_only=True)
      publisher_id = serializers.IntegerField(write_only=True)

      def create(self, validated_date):
        book = Book.objects.create(title=validated_data['title'],
        category=validated_data['post_category'], pub_time=validate_data['pub_time'],
        publisher_id=validated_data['publisher_id'])
        book.user.add(*validated_data['users'])
        return book

      反序列化views.py

    class BookView(APIView):
        def get(self, request):
            book_list = Book.objects.all()
            ret = BookSerializer(book_lilst, many=True)
            return Response(ret.data)
    
        def post(self, request):
            serializer = BookSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.validated_data)
            else:
                return Response(serializer.errors)
    

      当前端给我们发送patch请求的时候,前端传给我们用户要更新的数据,我们要对数据进行部分验证

      PATCH请求Serializers.py

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
      title = serializers.CharField(max_length=32)
      CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
      category = serializers.ChoiceField(choices=CHOICES,
        source='get_category_display',read_only=True)
      post_category = serializers.IntegerField(write_only=True)
      pub_time = serializers.DateField()
      
      publisher = PublisherSerializer(read_only=True)
      user = UserSerializer(many=True, read_only=True)

      users = serializers.ListField(write_only=True)
      publisher_id = serializers.IntegerField(write_only=True)

      def create(self, validated_date):
        book = Book.objects.create(title=validated_data['title'],
        category=validated_data['post_category'], pub_time=validate_data['pub_time'],
        publisher_id=validated_data['publisher_id'])
        book.user.add(*validated_data['users'])
        return book
      
      def update(self, instance, validated_data):
        instance.title = validated_data.get('title',instance.title)
        
        instance.pub_time = validated_data.get("pub_time", instance.pub_time)
    instance.category = validated_data.get("post_category", instance.category)
    instance.publisher_id = validated_data.get("publisher_id", instance.publisher_id)
    if validated_data.get("author_list"):
    instance.authors.set(validated_data["author_list"])
    instance.save()
    return instance

      put请求views.py

    # 更新函数
    class BookEditView(APIView):
        def put(self, request, id):
            book_obj = Book.objects.filter(id=id).first()
            ser_obj = BookSerializer(instance=book_obj, data=request.data, partial=True)
            if ser_obj.is_valid():
                ser_obj.save()
                return Response(ser_obj.validated_data)
            return Response(ser_obj.errors)  

    验证--钩子函数

      自定义验证钩子函数

    # validators验证器
    def my_validate(value): print(1111) if "敏感信息" in value.lower(): raise serializers.ValidationError("有敏感词汇") return value

      单个字段的钩子函数

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(required=False)
        # 自定义钩子函数的使用
        title = serializers.CharField(max_length=32, validators=[my_validate,])
        
        # 单个验证函数,title这个字段必须在要序列化的字段中
        # validate_字段名
        def validate_title(self, value):
            # value就是title的值 对value处理
            if "python" not in value.lower():
                raise serializers.ValidationError("标题必须含有python")
            return value
    

      多个字段的验证

        def validate(self, attrs):
            # attrs 是你传过来的所有的字段信息组成的字典
            if "python" in attrs["title"].lower() or attrs["post_category"] == 1:
                return attrs
            else:
                raise serializers.ValidationError("分类或标题不合符要求")
    

      权重关系: 

        自定义验证 > 单个字段的验证 > 多个字段的验证 

    ModelSerializer

      它会根据模型自动生成一组字段

      它简单的默认实现了.update()以及.create()方法

    定义一个ModelSerializer序列化

    class BookSerializer(serializers.ModelSerializer):
            class Meta:
            model = Book
            fields = "__all__"
            # exclude=["id"]
            # 会让你这些所有的外键关系变成read_only = True
    # depth 代表找嵌套关系的第几层, # depth = 1 # 外键关系的序列化 # 可以实现字段的不展现 extra_kwargs = {"publisher": {"write_only": True}, "authors": {"write_only": True}}

    SerializerMethodField

    外键关联的对象有很多字段我们是用不到的~都传给前端会有数据冗余~就需要我们自己去定制序列化外键对象的哪些字段~~

    class BookSerializer(serializers.ModelSerializer):
        # SerializerMethodField方法调用get_authors_info这个方法并把返回值传给publisher_info
        # 并在页面上展现出来
    pu publisher_info = serializers.SerializerMethodField(read_only=True) authors_info = serializers.SerializerMethodField(read_only=True) def get_authors_info(self, obj): # obj 就是序列化的每个Book对象 authors_querset = obj.authors.all() return [{"id": author.id, "name": author.name} for author in authors_querset] def get_publisher_info(self, obj): publisher_obj = obj.publisher return {"id": publisher_obj.id, "title": publisher_obj.title} class Meta: model = Book fields = "__all__" # exclude=["id"] # 会让你这些所有的外键关系变成read_only = True # depth = 1
    # 可以让一些字段不在页面上展现出来 extra_kwargs = {"publisher": {"write_only": True}, "authors":{"write_only": True}}

    POST请求:重写了create方法

    PUT/PATCH请求:重写update方法

    save() 方法其实是调用了create方法

      

  • 相关阅读:
    Java 练习(获取两个字符串中最大相同子串)
    STM32F103 实现 简易闹钟小程序
    STM32F103 实现 LCD显示年月日时分秒星期 并可逐值修改的日期 小程序
    Docker报错之“Failed to get D-Bus connection: Operation not permitted”
    数据结构解析
    每天一条DB2命令-004
    每天一条DB2命令-003
    每天一条DB2命令-002
    ElasticSearch系列
    模块三 GO语言实战与应用-BYTES包与字节串操作(下)
  • 原文地址:https://www.cnblogs.com/wjs521/p/9965207.html
Copyright © 2020-2023  润新知