自定义校验
class Student(models.Model):username = models.CharField(max_length=32)password = models.CharField(max_length=32)card = models.CharField(max_length=8)name = models.CharField(max_length=32)
class StudentLoginSerializer(serializers.Serializer):username = serializers.CharField(required=True, label="用户名")password = serializers.CharField(required=True, label="密码")def validate(self, attrs):username = attrs.get("username")password = attrs.get("password")user = models.Student.objects.filter(username=username, password=password).first()if user:refresh = RefreshToken.for_user(user)self.context["token"] = refreshreturn attrselse:raise ValidationError("用户名或密码错误")
# 登录签发tokenclass LoginViewSet(ViewSet):@action(methods=["POST"], detail=False)def login(self, request):login_ser = ser.StudentLoginSerializer(data=http://www.kingceram.com/post/self.request.data)if login_ser.is_valid():token_obj = login_ser.context["token"]return Response(data=http://www.kingceram.com/post/{"err": "", "code": 0,"access_token": str(token_obj.access_token),"refresh_token": str(token_obj)})else:return Response(data=http://www.kingceram.com/post/{"err": login_ser.errors.get("non_field_errors")[0],"code": 1,})
- win10 + centOS7双系统安装
- 新手必备 | 如何用U盘安装 Linux amp; Win7 双系统?
- 入职不到一年被提名晋升 ,历经双十一大考,10 年阿里老兵成长之路
- 青椒苗种植技术
- OpenSSL单向与双向认证通信
- 2支卡是什么意思
- 单向链表与双向链表
- 两群蜂怎么合并成双王群?
- 双人旁三点水亍读什么字
- 双色茉莉用深盆还是浅盆?