我有点迷失了...我正在尝试开发一个需要通过 Django Web 应用程序进行身份验证的 ionic 应用程序。
我安装了 Django-Rest-Framework 和 Django-Rest-Auth。我可以登录,使用 token 进入用户,但如何检索更多用户数据?
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-token-auth/$',obtain_auth_token),
使用 host.com/rest-auth/user/url 中的 Django-rest-auth,我只有以下数据:
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, PUT, HEAD, OPTIONS, PATCH
{
"username": "ikerib",
"email": "ikerib@gmail.com",
"first_name": null,
"last_name": null
}
但我需要更多!例如 ID、用户照片、城市...
我尝试像这样配置用户序列化器:
from rest_framework import serializers
from gamer.models import GamerUser
class GameUserSerializer(serializers.ModelSerializer):
class Meta:
model = GamerUser
fields = ('id', 'fullname', 'get_photo', 'karma')
这个 View :
@csrf_exempt
def gameuser_detail(request, pk):
try:
gameuser = GameUser.objects.get(pk=pk)
except GameUser.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = GameUserSerializer(gameuser)
return JSONResponse(serializer.data)
但我无法获取用户数据,因为我不知道用户 ID..
有什么帮助或线索吗???
提前致谢
最佳答案
您可以简单地将自定义序列化器添加到您的应用设置中,如下所示:
REST_AUTH_SERIALIZERS = {
'USER_DETAILS_SERIALIZER': 'path.to.custom.GameUserSerializer',
}
http://django-rest-auth.readthedocs.org/en/latest/configuration.html
关于django Rest-auth 获取用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33464502/