aircraft
aircraft

Reputation: 26876

After login the `rest-auth`, how to return more information?

I use django-rest-auth in my Django project.

After login the rest-auth/login/, how to return more information?

In the rest-auth/login/, when I login the user, it returns a key.

enter image description here

I want to also return the user's information, how can I get this?

Upvotes: 14

Views: 2625

Answers (3)

Damir Talipov
Damir Talipov

Reputation: 119

I was using the latest version (6.0.0) and the solution that was suggested above didn't work for me, so I did some research and figured out that issue was coming from settings.py.

the approach that was described in answer worked till dj-rest-auth <= 2.2.8 versions

REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
    ...
}

Docs example of right configuration dj-rest-auth <= 2.2.8 versions

for dj-rest-auth > 2.2.8 version you have to specify like this in your settings.py

REST_AUTH = {
    'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
    ...
}

Docs example of right configuration dj-rest-auth > 2.2.8 versions

The same question was asked by me recently, due to the fact that approach suggested above was outdated, managed to tackle by myself and I'm sharing the solution from my own question hope if it will help somebody.

Upvotes: 1

Greko2015 GuFn
Greko2015 GuFn

Reputation: 589

Please refer to this link

You can override the default TokenSerializer with a custom serializer that will include users.

in a file say yourapp/serializers.py

from django.conf import settings

from rest_framework import serializers
from rest_auth.models import TokenModel
from rest_auth.utils import import_callable
from rest_auth.serializers import UserDetailsSerializer as DefaultUserDetailsSerializer

# This is to allow you to override the UserDetailsSerializer at any time.
# If you're sure you won't, you can skip this and use DefaultUserDetailsSerializer directly
rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
UserDetailsSerializer = import_callable(
    rest_auth_serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer)
)

class CustomTokenSerializer(serializers.ModelSerializer):
    user = UserDetailsSerializer(read_only=True)

    class Meta:
        model = TokenModel
        fields = ('key', 'user', )

and in your app settings use rest-auth configuration to override the default class

yourapp/settings.py

REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'yourapp.serializers.CustomTokenSerializer' # import path to CustomTokenSerializer defined above.
}

Upvotes: 2

aircraft
aircraft

Reputation: 26876

At last, I get my solution:

class TokenSerializer(serializers.ModelSerializer):
    """
    Serializer for Token model.
    """
    user = UserInfoSerializer(many=False, read_only=True)  # this is add by myself.
    class Meta:
        model = TokenModel
        fields = ('key', 'user')   # there I add the `user` field ( this is my need data ).

In the project settings.py, add the TOKEN_SERIALIZER like bellow:

REST_AUTH_SERIALIZERS = {
    ...
    'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
}

Now I get my need data:

enter image description here

Upvotes: 16

Related Questions