Dil*_*ani 5 django django-registration django-rest-framework
我正在使用Djando rest_auth.registration.
我在urls.py中的相应条目是
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
Run Code Online (Sandbox Code Playgroud)
我的身份验证类是rest_framework.authentication.TokenAuthentication
其余的API调用非常有效.
当我通过此API注册时,我得到以下响应.
{
"key": "3735f13cd69051579156f98ffda338a2d7a89bb5"
}
Run Code Online (Sandbox Code Playgroud)
我还想在响应中包含user_id字段.我该怎么做呢 我尝试从类RegisterView(CreateAPIView)扩展方法get_response_data:但无法这样做.有人可以建议实现这一目标的最佳做法.代码是理想的.谢谢.
我想使用rest_auth.registration提供的rest-auth/registration/url.我不想为此创建单独的新URL.
我的Settings.py如下
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'sdAPI.apps.SdapiConfig',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_framework_swagger',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
'django_extensions',
]
# auth and allauth settings
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['email', 'publish_stream'],
'METHOD': 'oauth2' # instead of 'oauth2'
}
}
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)
}
SITE_ID = 1
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
REST_SESSION_LOGIN = False
Run Code Online (Sandbox Code Playgroud)
我的urls.py如下
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^user/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/',include('rest_auth.registration.urls')),
]
Run Code Online (Sandbox Code Playgroud)
我认为您只需要覆盖配置中的TOKEN_SERIALIZER选项REST_AUTH_SERIALIZERS.
from rest_framework.authtoken.models import Token
class TokenSerializer(serializers.ModelSerializer):
class Meta:
model = Token
fields = ('key', 'user')
Run Code Online (Sandbox Code Playgroud)
然后,settings.py按照文档中的说明将其设置为
REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
'TOKEN_SERIALIZER': 'path.to.custom.TokenSerializer',
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |