标签:validate nal 钩子 方法 errors view 代码 with attrs
def post(self, request, *args, **kwargs):
# 新增
response = {'status': 100, 'msg': '新增成功'}
book_ser = BookSerializer(data=request.data)
# 提交的字段校验通过
if book_ser.is_valid():
book_ser.save()
response['book'] = book_ser.data
else:
response['msg'] = book_ser.errors
return Response(response)
#局部钩子
def validate_title(self,value):
from rest_framework import exceptions
if value.startswith('sb'):
raise exceptions.ValidationError('xxx')
return value
#全局
def validate(self, attrs):
from rest_framework import exceptions
if attrs.get('title')== attrs.get('title2'):
return attrs
else:
raise exceptions.ValidationError('不想等啊')
流程总结:
标签:validate nal 钩子 方法 errors view 代码 with attrs
原文地址:https://www.cnblogs.com/wanlei/p/10426517.html