diff options
Diffstat (limited to 'rest_framework/authtoken')
| -rw-r--r-- | rest_framework/authtoken/migrations/0001_initial.py | 7 | ||||
| -rw-r--r-- | rest_framework/authtoken/serializers.py | 7 | ||||
| -rw-r--r-- | rest_framework/authtoken/views.py | 3 |
3 files changed, 9 insertions, 8 deletions
diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 2e5d6b47..769f6202 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# encoding: utf8 +# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations @@ -15,12 +15,11 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Token', fields=[ - ('key', models.CharField(max_length=40, serialize=False, primary_key=True)), - ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL, to_field='id')), + ('key', models.CharField(primary_key=True, serialize=False, max_length=40)), ('created', models.DateTimeField(auto_now_add=True)), + ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL, related_name='auth_token')), ], options={ - 'abstract': False, }, bases=(models.Model,), ), diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index 99e99ae3..c2c456de 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -19,11 +19,12 @@ class AuthTokenSerializer(serializers.Serializer): if not user.is_active: msg = _('User account is disabled.') raise serializers.ValidationError(msg) - attrs['user'] = user - return attrs else: - msg = _('Unable to login with provided credentials.') + msg = _('Unable to log in with provided credentials.') raise serializers.ValidationError(msg) else: msg = _('Must include "username" and "password"') raise serializers.ValidationError(msg) + + attrs['user'] = user + return attrs diff --git a/rest_framework/authtoken/views.py b/rest_framework/authtoken/views.py index 7c03cb76..94e6f061 100644 --- a/rest_framework/authtoken/views.py +++ b/rest_framework/authtoken/views.py @@ -18,7 +18,8 @@ class ObtainAuthToken(APIView): def post(self, request): serializer = self.serializer_class(data=request.DATA) if serializer.is_valid(): - token, created = Token.objects.get_or_create(user=serializer.object['user']) + user = serializer.validated_data['user'] + token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key}) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
