类视图的装饰器
https://blog.csdn.net/qq_42684307/article/details/81042845
jwt 验证
https://mp.weixin.qq.com/s?__biz=MjM5OTMyODA4Nw%3D%3D&chksm=a73c6653904bef458c2baffaf87d93960034b54e8a999b54548b5937116e6d40381360dcac28&idx=1&mid=2247485035&scene=21&sn=c437e8211f375e2e06de2498127e821a#wechat_redirect 
序列化器自动添加创建人和修改人字段
def to_internal_value(self, data):
# data为未验证的数据,对其添加上 update_by
data["update_by"] = self.context["request"].user.username
if self.context["request"]._request.method == "POST":
data["create_by"] = self.context["request"].user.username
return super().to_internal_value(data)
序列化器的校验机制
调用序列化器对象的is_valid()方法,校验前端参数的正确性,不调用则不校验
校验成功返回True、校验失败返回False
is_valid(raise_exception = True):校验失败后,则抛出异常
当调用is_valid()之后,才能调用序列化器对象的errors属性,内容为校验的错误提示(dict)
在views.py中,如果传参进行了输入反序列化的话,那么需要调用的是经过校验后的数据,比如说新增数据,应该是:xxx类.objects.create(**serializer.validated_data)
验证器的第一种实现方式

自定义校验器的使用

自定义单个字段的校验

自定义多字段的校验

校验顺序
字段定义时候的限制,如参数类型,字段长度,是否可以为空等;
自定义的包含validators的列表从左到右边进行校验;
validate_字段名单字段校验;
validate多字段校验
|