1、序列化数据:
创建serializers.py的python文件
从rest_framework里导入serializers类:
from rest_framework import serializers
class LoginSerializer(serializers.Serializer):
email = serializers.EmailField(required=True, error_messages={"required": "请输入邮箱!"})
password = serializers.CharField(max_length=20, min_length=6)
# 重写校验
def validate(self, attrs):
email = attrs.get('email')
password = attrs.get('password')
if email and password:
user = OAUser.objects.filter(email=email).first()
if not user:
raise serializers.ValidationError("请输入正确的邮箱!")
if not user.check_password(password):
raise serializers.ValidationError("请输入正确的密码!")
# 判断状态
if user.status == UserStatusChoices.UNACTIVE: