[英]How to skip or remove password field in simplejwt token authentication in django rest framework?
My requirement is, I don't wanted to enter password in simplejwt token authentication.我的要求是,我不想在 simplejwt 令牌身份验证中输入密码。 I have added one extra field in the authentication by inheriting the init()
method of TokenObtainPairSerializer as per my requrements.根据我的要求,我通过继承TokenObtainPairSerializer的init()
方法在身份验证中添加了一个额外的字段。
Currently, I am passing None
as in password field but still its showing to user (djnago admin portal).目前,我在密码字段中传递None
,但仍向用户显示(djnago 管理门户)。 What I want is, I don't wanted to show the password field to user while authentication using simplejwt.我想要的是,我不想在使用 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?有什么方法可以将 PasswordField 设置为不可用,这样它就不会显示给用户?
I have followed the below mentioned process to solve the problem,我已经按照下面提到的过程来解决问题,
Override the TokenObtainPairSerializer
class __init__
method like below,覆盖TokenObtainPairSerializer
class __init__
方法,如下所示,
Use del self.fields['password']
, so It wont ask you the password and add whatever fields you want.使用del self.fields['password']
,因此它不会询问您密码并添加您想要的任何字段。
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.让我知道是否有人对此问题有更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.