问题
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,
Override the
TokenObtainPairSerializer
class__init__
method like below,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