动态用户名\字段djangorestframework和simplejwt

动态用户名\字段djangorestframework和simplejwt,django,django-rest-framework,django-serializer,django-rest-framework-simplejwt,Django,Django Rest Framework,Django Serializer,Django Rest Framework Simplejwt,我正在开发一个身份验证应用程序,用户可以通过 (email or mobile) and (password or otp) 使用的框架/库 Django Rest Framework and djangorestframework-simplejwt 我试图在生成的jwt令牌中添加多个声明 下面是我的LoginView和LoginSerializer 看法 序列化程序 class LoginSerializer(TokenObtainPairSerializer): mobile

我正在开发一个身份验证应用程序,用户可以通过

(email or mobile) and (password or otp)
使用的框架/库

Django Rest Framework and djangorestframework-simplejwt
我试图在生成的jwt令牌中添加多个声明

下面是我的LoginView和LoginSerializer

看法

序列化程序

class LoginSerializer(TokenObtainPairSerializer):
    mobile = serializers.CharField(allow_blank=True)
    email = serializers.EmailField(allow_blank=True)
    password = serializers.CharField(allow_blank=True)
    otp = serializers.CharField(allow_blank=True)

    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        token['name'] = user.first_name
        return token

    def validate(self, attrs):
        mobile = attrs.get("mobile", None)
        email = attrs.get("email", None)
        password = attrs.get("password", None)
        otp = attrs.get("otp", None)

        user = authenticate(mobile=mobile, email=email, password=password, otp=otp)

        if user is None and self.password:
            raise serializers.ValidationError(
            detail="Incorrect Username or Password.", code=HTTP_401_UNAUTHORIZED
        )

        if user.is_active:
            refresh = self.get_token(user)
            data = dict()
            data['refresh'] = str(refresh)
            data['access'] = str(refresh.access_token)
            return data

        if user.is_locked:
            raise serializers.ValidationError(
            detail="Account Locked. Contact Support.", code=HTTP_423_LOCKED
        )

        raise serializers.ValidationError(
            detail="User Account is Deactivated.", 
code=HTTP_401_UNAUTHORIZED
    )
但我收到的电子邮件不能是空白错误时,发送一个有效的电话号码和密码的要求。这是因为TokenActainPairSerializer检查User.USERNAME_字段(在我的例子中是电子邮件)

我如何处理这种情况或使其工作

class LoginSerializer(TokenObtainPairSerializer):
    mobile = serializers.CharField(allow_blank=True)
    email = serializers.EmailField(allow_blank=True)
    password = serializers.CharField(allow_blank=True)
    otp = serializers.CharField(allow_blank=True)

    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        token['name'] = user.first_name
        return token

    def validate(self, attrs):
        mobile = attrs.get("mobile", None)
        email = attrs.get("email", None)
        password = attrs.get("password", None)
        otp = attrs.get("otp", None)

        user = authenticate(mobile=mobile, email=email, password=password, otp=otp)

        if user is None and self.password:
            raise serializers.ValidationError(
            detail="Incorrect Username or Password.", code=HTTP_401_UNAUTHORIZED
        )

        if user.is_active:
            refresh = self.get_token(user)
            data = dict()
            data['refresh'] = str(refresh)
            data['access'] = str(refresh.access_token)
            return data

        if user.is_locked:
            raise serializers.ValidationError(
            detail="Account Locked. Contact Support.", code=HTTP_423_LOCKED
        )

        raise serializers.ValidationError(
            detail="User Account is Deactivated.", 
code=HTTP_401_UNAUTHORIZED
    )