jpadilla / django-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework
http://jpadilla.github.io/django-rest-framework-jwt/
MIT License
3.19k stars 652 forks source link

AttributeError when using custom UserDetailsSerializer #314

Closed anarute closed 7 years ago

anarute commented 7 years ago

We have created a custom UserDetailsSerializer but when trying to run the application we get the error:

AttributeError: module 'path.to.serializers' has no attribute 'MPUserDetailsSerializer'

The full error is pasted here.

The setup for REST_AUTH_SERIALIZERS and USER_DETAILS_SERIALIZER in the django settings is:

REST_AUTH_SERIALIZERS = {
    'LOGIN_SERIALIZER': 'questions.serializers.LoginSerializer',
    'JWT_SERIALIZER' : 'questions.serializers.JWTSerializer',
    'USER_DETAILS_SERIALIZER' : 'questions.serializers.MPUserDetailsSerializer',
}

The custom serializer is:

from rest_auth import serializers as auth_serializers

class MPUserDetailsSerializer(auth_serializers.UserDetailsSerializer):
"""
User model w/o password
"""
class Meta:
    model = User
    fields = ('pk', 'username', 'email', 'name')
    read_only_fields = ('username', )

def validate_email(self, email):
    email = get_adapter().clean_email(email)
    if allauth_settings.UNIQUE_EMAIL:
        if email and email_address_exists(email):
            raise serializers.ValidationError(
                _("A user is already registered with this e-mail address."))
    return email

The workaround that we did was to remove the following lines from our local virtualenv, in the file /lib/python3.5/site-packages/rest-auth/serializers.py:

# Required to allow using custom USER_DETAILS_SERIALIZER  in
# JWTSerializer. Defining it here to avoid circular imports
rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
JWTUserDetailsSerializer = import_callable(
  rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer)
)

And replacing JWTUserDetailsSerializer with UserDetailsSerializer in the same file. But that's a really bad solution since we are changing third parties code and it doesn't make any sense to remove specifically the lines that are told to be required to allow custom USER_DETAILS_SERIALIZER, but we don't know what else to do to make it work, are we missing something? a config maybe?

We're using django v1.10.1, djangorestframework v3.4.7 and django-rest-auth v0.9.0

(This issue was firstly created at stackoverflow)

jpadilla commented 7 years ago

@anarute Isn't this an issue with django-rest-auth?

anarute commented 7 years ago

@jpadilla you're right, I'm sorry about that.