drf serializer

Third-rate

import sys
#标准输出流
sys.stdout.write('123\n')
sys.stdout.write('456\n')
#标准输入流
res=sys.stdin.readline()
print(res)
#标准错误流:将括号内数据作为错误输出
sys.srderr.write('abc\n')
sys.stderr.write('xyz\n)

Note: The author is an asynchronous between each stream, synchronous submitted within the stream.

JSON file format

JSON values ​​can be:
  • Number (integer or floating point)
  • String (in double quotes)
  • A logical value (true or false)
  • An array (in square brackets)
  • Objects (in braces)
  • null
JSON syntax rules:
  • Data in the name / value pairs
  • Data separated by commas
  • Save Object braces
  • Save array square brackets

Why use serializer

A method in response drf only receive json number format and sending the data analysis json binary format to the head end, may be a more convenient method of more data validation and database operations.

Serialization family

  1. Serializer class is serialized underlying class (learn how)
  2. ModelSerializer class, class model sequence (core)
  3. ListSerializer class: class operation sequence group (auxiliary class)

Manual serialization

Front and rear side interaction, file transfer format json by using djangoORM statement from the database to find out the object to interact manually Switch json serialization format then interact.

class UserV1APIView(APIView):
    # 单查群查
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk:
            user_dic = models.User.objects.filter(is_delete=False, pk=pk).values('username', 'sex', 'img').first()
            if not user_dic:
                return Response({
                    'status': 1,
                    'msg': 'pk error',
                }, status=400)

            user_dic['img'] = '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, user_dic.get('img'))
            return Response({
                'status': 0,
                'msg': 'ok',
                'results': user_dic
            })
        else:
            user_query = models.User.objects.filter(is_delete=False).values('username', 'sex', 'img').all()
            for user_dic in user_query:
                user_dic['img'] = '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, user_dic.get('img'))
            user_list = list(user_query)
            return Response({
                'status': 0,
                'msg': 'ok',
                'results': user_list
            })

Serializer class sequence

Serialization

Serializers.py new file, the file operation class Serializer

from rest_framework import serializers
from django.conf import settings
from . import models
class UserSerializer(serializers.Serializer):
    # 1)字段名与字段类型要与处理的model类对应
    # 2)不提供的字段,就不参与序列化给前台
    # 3)可以自定义序列化字段,采用方法序列化,方法固定两个参数,第二个参数就是参与序列化的model对象
    #       (严重不建议自定义字段名与数据库字段名重名,由get_自定义字段名方法的返回值提供字段值)
    username = serializers.CharField()
    # sex = serializers.IntegerField()
    # sex = serializers.SerializerMethodField()  # 不建议这样命名
    gender = serializers.SerializerMethodField()

    def get_gender(self, obj):
        return obj.get_sex_display()

    # 注:在高级序列化与高级视图类中,drf默认帮我们处理图片等子资源
    icon = serializers.SerializerMethodField()

    def get_icon(self, obj):
        return '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, obj.img)

Use renamed display field, defined in the official recommended method attribute class models

#models中
    # 第二种自定义序列化字段(插拔式,提倡使用)
    @property
    def gender(self):
        return self.get_sex_display()

    @property
    def icon(self):
        from django.conf import settings
        return '%s%s%s' % (settings.BASE_URL, settings.MEDIA_URL, self.img)

to sum up:

  1. Set serializable fields, field names and field types for a process model corresponding to the class name attribute (type need not only participate in serialization setting conditions)

  2. Some model class field, but not in the sequence of the corresponding field, such fields do not participate serialized

  3. Custom serialization field (a method), the type field is SerializerMethodField (), values ​​are get_ custom field name (self, model_obj) method provided,

    Typical values ​​are associated with model objects (model_obj) involved in serialized

Deserialization

class UserDeSerializer(serializers.Serializer):
    # 系统校验字段
    username = serializers.CharField(min_length=3, max_length=16, error_messages={
        'min_length': '太短',
        'max_length': '太长'
    })
    password = serializers.CharField(min_length=3, max_length=16)

    # 不写,不参与反序列化,写就必须参与反序列化(但可以设置required=False取消必须)
    # required=False的字段,前台不提供,走默认值,提供就一定进行校验;不写前台提不提供都采用默认值
    sex = serializers.BooleanField(required=False)

    # 自定义校验字段:从设置语法与系统字段没有区别,但是这些字段不能参与入库操作,需要在全局钩子中,将其取出
    re_password = serializers.CharField(min_length=3, max_length=16)

    # 局部钩子:
    #   方法就是 validate_校验的字段名(self, 校验的字段数据)
    #   校验规则:成功直接返回value,失败抛出校验失败信息
    def validate_username(self, value):
        if 'g' in value.lower():
            raise serializers.ValidationError('名字中不能有g')
        return value

    # 全局钩子:
    #   方法就是 validate(self, 所有的校验数据)
    #   校验规则:成功直接返回attrs,失败抛出校验失败信息
    def validate(self, attrs):
        password = attrs.get('password')
        re_password = attrs.pop('re_password')
        if password != re_password:
            raise serializers.ValidationError({'re_password': '两次密码不一致'})
        return attrs

    # 在视图类中调用序列化类的save方法完成入库,Serializer类能做的增入库走create方法,改入库走update方法
    # 但Serializer没有提供两个方法的实现体
    def create(self, validated_data):
        return models.User.objects.create(**validated_data)

    # instance要被修改的对象,validated_data代表校验后用来改instance的数据
    def update(self, instance: models.User, validated_data):
        # 用户名不能被修改
        validated_data.pop('username')
        models.User.objects.filter(pk=instance.id).update(**validated_data)
        return instance

For checking data, need to customize the local and the global hook by a method to customize the system for verification method is also needed to write and create their own update method.

ModelSerializer sequence class

class UserModelSerializer(serializers.ModelSerializer):
    # 第一种自定义序列化字段:该字段必须在fields中设置
    # gender = serializers.SerializerMethodField()
    # def get_gender(self, obj):
    #     return obj.get_sex_display()


    # 自定义反序列化字段同Serializer类,且规则只能在此声明中设置,或是在钩子中设置,
    #       在extra_kwargs中对其设置的无效
    # 注:自定义反序列化字段与系统字段,设置规则一样,所以必须设置 write_only
    re_password = serializers.CharField(min_length=3, max_length=16, write_only=True)

    class Meta:
        model = models.User
        # fields采用 插拔式 设置所有参与序列化与反序列化字段
        fields = ('username', 'gender', 'icon', 'password', 'sex', 're_password')
        extra_kwargs = {
            'username': {  # 系统字段不设置read_only和write_only,默认都参加
                'min_length': 3,
                'max_length': 10,
                'error_messages': {
                    'min_length': '太短',
                    'max_length': '太长'
                }
            },
            'gender': {
                'read_only': True,  # 自定义的序列化字段默认就是read_only,且不能修改,可以省略
            },
            'password': {
                'write_only': True,
            },
            'sex': {  # 像sex有默认值的字段,为选填字段('required': True可以将其变为必填字段)
                'write_only': True,
                # 'required': True
            }
        }


    # 局部全局钩子同Serializer类,是与 Meta 同缩进的
    def validate_username(self, value):
        if 'g' in value.lower():
            raise serializers.ValidationError('名字中不能有g')
        return value

    def validate(self, attrs):
        password = attrs.get('password')
        re_password = attrs.pop('re_password')
        if password != re_password:
            raise serializers.ValidationError({'re_password': '两次密码不一致'})
        return attrs

    # create和update方法不需要再重写,ModelSerializer类已提供,且支持所有关系下的连表操作

to sum up:

  1. Serialization class inheritance ModelSerializer, it needs to be configured in the configuration class Meta
  2. Configuration model: Model binding sequence of related tables
  3. configuration fields: The plug provided all participating deserialization and serialization field
  4. extra_kwargs configuration: The system is divided into three fields: read-only (read_only), write only (write_only), read-write (not set) field is necessary: ​​required optional fields: configuration in extra_kwargs, without setting required, and have default values
  5. Custom serialization fields: a first (not promote): In the class with the sequence SerializerMethodField () to implement the second (promote): In class model with @property achieved, pluggable
  6. Custom deserialize field: only this statement to set the same Serializer classes and rules, or to set the hook, the invalid custom field and de-serialization system in its field in extra_kwargs set as setting rules, so we must set write_only
  7. Local hooks, hooks with the global class Serializer
  8. You do not need to rewrite the create and update methods

Guess you like

Origin www.cnblogs.com/agsol/p/12099445.html