3.反序列化器的新增、更新功能

1.定义create方法直接在数据库中进行新增

from rest_framework import serializers
from Plate.models import User
 
 
class Userserializer(serializers.Serializer):
    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)
 
    def validate_name(self, value):
        if value == "徐骁":
            raise serializers.ValidationError('姓名不能传入徐骁')
        return value
 
    def validate(self, data):
        if data.get('addr') == '武帝城':
            raise serializers.ValidationError('地址不能输入武帝城')
        elif data.get('

你可能感兴趣的:(数据库,python,开发语言)