在 django-rest-auth 中使用自定义 UserDetailsSerializer 时出现 AttributeError
AttributeError when using custom UserDetailsSerializer in django-rest-auth
我们已经创建了一个自定义 UserDetailsSerializer
但是在尝试 运行 应用程序时我们收到错误:
AttributeError: module 'path.to.serializers' has no attribute 'MPUserDetailsSerializer'
完整的错误是pasted here。
django 设置中 REST_AUTH_SERIALIZERS
和 USER_DETAILS_SERIALIZER
的设置是:
REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER': 'questions.serializers.LoginSerializer',
'JWT_SERIALIZER' : 'questions.serializers.JWTSerializer',
'USER_DETAILS_SERIALIZER' : 'questions.serializers.MPUserDetailsSerializer',
}
自定义序列化器是:
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
我们所做的解决方法是从本地 virtualenv
文件 /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)
)
并在同一文件中用 UserDetailsSerializer
替换 JWTUserDetailsSerializer
。
我知道更改第三方代码不是一个好的做法,并且专门删除被告知需要允许自定义的行没有任何意义 USER_DETAILS_SERIALIZER
,但我们不会知道还需要做些什么才能让它发挥作用,我们是否遗漏了什么?也许是一个配置?
我们使用的是 django v1.10.1、djangorestframework v3.4.7 和 django-rest-auth v0.9.0
这是 django-rest-auth
的一个问题,已在最新版本 - 0.9.1
中修复
要查看更多,check this issue。
我们已经创建了一个自定义 UserDetailsSerializer
但是在尝试 运行 应用程序时我们收到错误:
AttributeError: module 'path.to.serializers' has no attribute 'MPUserDetailsSerializer'
完整的错误是pasted here。
django 设置中 REST_AUTH_SERIALIZERS
和 USER_DETAILS_SERIALIZER
的设置是:
REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER': 'questions.serializers.LoginSerializer',
'JWT_SERIALIZER' : 'questions.serializers.JWTSerializer',
'USER_DETAILS_SERIALIZER' : 'questions.serializers.MPUserDetailsSerializer',
}
自定义序列化器是:
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
我们所做的解决方法是从本地 virtualenv
文件 /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)
)
并在同一文件中用 UserDetailsSerializer
替换 JWTUserDetailsSerializer
。
我知道更改第三方代码不是一个好的做法,并且专门删除被告知需要允许自定义的行没有任何意义 USER_DETAILS_SERIALIZER
,但我们不会知道还需要做些什么才能让它发挥作用,我们是否遗漏了什么?也许是一个配置?
我们使用的是 django v1.10.1、djangorestframework v3.4.7 和 django-rest-auth v0.9.0
这是 django-rest-auth
的一个问题,已在最新版本 - 0.9.1
要查看更多,check this issue。