我正在使用 django-rest-framework-simplejwt 进行用户注册。
按照本教程在此处输入链接描述
我的代码如下:
class RegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(
style={'input_type': 'password'}, write_only=True,
)
password2 = serializers.CharField(
style={'input_type': 'password'},max_length=20
)
tokens = serializers.SerializerMethodField()
class Meta:
model = UserProfile
fields = ['username', 'email', 'password', 'password2', 'tokens']
def get_tokens(self, user):
user = UserProfile(
email=self.validated_data['email'],
username=self.validated_data['username']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
user.set_password(password)
tokens = RefreshToken.for_user(user)
refresh = text_type(tokens)
access = text_type(tokens.access_token)
data = {
"refresh": refresh,
"access": access
}
return data
def save(self):
user = UserProfile(
email=self.validated_data['email'],
username=self.validated_data['username']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
user.set_password(password)
user.save()
return user
鉴于:
class UserCreateView(generics.CreateAPIView):
'''create user'''
serializer_class = RegistrationSerializer
问题是每次我创建一个用户时,我都可以获得 2 两个令牌的返回,但是在数据库中我找不到令牌。
所以我想我没有存储它们,所以我应该存储令牌吗?