4.模型序列化器ModelSerializer

1.定义模型序列化器,直接继承自ModelSerializer,其它代码无需变更

from rest_framework import serializers

from Plate.models import User



class Userserializer(serializers.ModelSerializer):

    # id = serializers.IntegerField(label='ID', read_only=True)
    # name = serializers.CharField(max_length=4, label='姓名')
    # password = serializers.CharField(max_length=20, label='密码')
    # sex = serializers.BooleanField(label='性别')
    # addr = serializers.CharField(max_length=100, label='地址', allow_blank=True)

    class Meta:
        model = User
        fields = "__all__"
        # exclude=("sex")  # 排除掉哪些字段
        # read_only_fields=("id")  # 只用作序列化器输出,不做编辑保存操作
        # extra_kwargs = {"name": {"min_value": 0, "required": False}}  # 对参数进行额外验证


    def va

你可能感兴趣的:(后端,django,python)