How to skip or remove password field in simplejwt token authentication in django rest framework?

最后都变了- 提交于 2020-07-09 16:08:09

问题


My requirement is, I don't wanted to enter password in simplejwt token authentication. I have added one extra field in the authentication by inheriting the init() method of TokenObtainPairSerializer as per my requrements.

Currently, I am passing None as in password field but still its showing to user (djnago admin portal). What I want is, I don't wanted to show the password field to user while authentication using simplejwt.

below is my code,

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class CustomSerializer(TokenObtainPairSerializer):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields[self.username_field] = serializers.CharField()
        self.fields['password'] = PasswordField(default=None)
        self.fields['extra'] = serializers.CharField()

    def validate(self, attrs):
        pass

Is there any ways to set PasswordField as unusable so it wont show to user?


回答1:


I have followed the below mentioned process to solve the problem,

  1. Override the TokenObtainPairSerializer class __init__ method like below,

  2. Use del self.fields['password'], so It wont ask you the password and add whatever fields you want.

class CustomSerializer(TokenObtainPairSerializer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() del self.fields['password']

This works really well. There is a almost same question I have answered, You can check it here for more knowledge.

Let me know if anyone have better solution of this problem.



来源:https://stackoverflow.com/questions/61475301/how-to-skip-or-remove-password-field-in-simplejwt-token-authentication-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!