14π
β
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:
π€aircraft
2π
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.
}
π€Greko2015 GuFn
- Django: Display a custom error message for admin validation error
- Order in django migrations
- Fail to push to Heroku: /app/.heroku/python/bin/pip:No such file or directory
- Pip install Django on python3.6
Source:stackexchange.com