我们已经创建了一个自定义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)
)
在同一个文件中将JWTUserDetailsSerializer
替换为UserDetailsSerializer
。
我知道改变第三方代码并不是一个好习惯,而且删除那些被告知需要自定义USER_DETAILS_SERIALIZER
的行是没有任何意义的,但我们不知道whatelse要做到这一点,我们是否遗漏了什么?配置可能吗?
我们正在使用django v1.10.1,djangorestframework v3.4.7和django-rest-auth v0.9.0