校验
class BookJson(serializers.ModelSerializer):
class Meta:
model = models.Book
fields = '__all__'
name = serializers.CharField(min_length=3, max_length=5, error_messages={'min_length': '太短'})
def validate_name(self, value):
from rest_framework.exceptions import ValidationError
if 'sb' in value:
raise ValidationError('不能出现哔词汇')
return value
class Books(APIView):
def post(self, request):
book = request.data
book_json = objson.BookJson(data=book)
if book_json.is_valid():
book_json.save()
return Response({
'status': 0,
'msg': 'ok',
'results': book_json.data
})
return Response({
'status': 1,
'msg': book_json.errors,
})
了解
def validate(self, attrs):
from rest_framework.exceptions import ValidationError
pwd = attrs.get('pwd')
re_pwd = attrs.get('re_pwd')
if pwd == re_pwd:
return attrs
else:
raise ValidationError('密码校验失败')
认证、频率
class Auth:
def authenticate(self, request):
pass
class Book(APIView):
authentication_classes = [Auth, ]
https://blog.csdn.net/linwow/article/details/94589155#_148