[Django]-Token.objects not found

2👍

Try to do the following

>>> from rest_framework.authtoken.models import Token
>>> Token.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Token' has no attribute 'objects'

If the above error occurs, That’s because you didn’t add the auth token in the settings’ INSTALLED_APPS.
if it’s not the in the INSTALLED_APPS, it’s abstract and doesn’t have the default manager (objects).

1👍

Looks like the Token model is not created. So make sure you followed the installation in the documentation.

Make sure you have 'rest_framework.authtoken' in INSTALLED_APPS

Also, rest framework authentication classes inside settings.py

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated',
)

}

Also, make sure you do python manage.py makemigrations and then python manage.py migrate which create the Token model

👤Arun K

0👍

First check whether you have added 'rest_framework.authtoken' under INSTALLED_APPS in settings.py. Then run ‘python manage.py migrate’.

Secondly you should open new python shell while creating token for users without token as follows:

>>> `from django.contrib.auth.models import User`
>>> `from rest_framework.authtoken.models import Token`
>>> `user = User.objects.get(pk=1)`                        
>>> `Token.objects.create(user=user)`

<Token: efa5fc4a8b142cd20478a7baaf9d763be44d6acc>

Leave a comment