From dd6db3563f9205e4a42d171f25948cc110e13b8b Mon Sep 17 00:00:00 2001 From: Maxim Kukhtenkov Date: Sun, 27 Nov 2016 17:57:55 -0800 Subject: [PATCH] Allow using custom UserDetailsSerializer with JWTSerializer - update --- rest_auth/serializers.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 864b016..4d5d194 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -6,11 +6,12 @@ from django.utils.http import urlsafe_base64_decode as uid_decoder from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_text -from .models import TokenModel - from rest_framework import serializers, exceptions from rest_framework.exceptions import ValidationError +from .models import TokenModel +from .utils import import_callable + # Get the UserModel UserModel = get_user_model() @@ -130,21 +131,20 @@ class UserDetailsSerializer(serializers.ModelSerializer): read_only_fields = ('email', ) +# Required to allow using custom UserDetailsSerializer in +# JWTSerializer +rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {}) +JWTUserDetailsSerializer = import_callable( + rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer) +) + + class JWTSerializer(serializers.Serializer): """ Serializer for JWT authentication. """ token = serializers.CharField() - - def __init__(self, *args, **kwargs): - """ - Need to add `user` field dynamically, to allow using - custom UserDetailsSerializer - """ - from app_settings import UserDetailsSerializer - - super(JWTSerializer, self).__init__(*args, **kwargs) - self.fields['user'] = UserDetailsSerializer() + user = JWTUserDetailsSerializer() class PasswordResetSerializer(serializers.Serializer):