Monday, 12 August 2013

When trying to create Token object from Django shell in Django REST Framework, I get "AttributeError: type object 'Token' has no attribute...

When trying to create Token object from Django shell in Django REST
Framework, I get "AttributeError: type object 'Token' has no attribute...

I'm trying to create an API token for a user from the Django shell, but
when I do I get an error instead of being able to generate the token. This
is the relevant code:
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
user = User.objects.filter(username__exact="test_superuser")[0]
token = Token.objects.create(user=user)
Rather than generating a token for test_superuser, instead this code gives
me the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Token' has no attribute 'objects'

4 comments:

  1. I get the same error. Did you find a workaround?

    ReplyDelete
  2. It 's because you do not import the rest_framework.authtoken model. Check the settings.py you are loading (with debug_toolbar for example) and fix it.

    ReplyDelete
  3. Be sure to add "rest_framework.authtoken" to INSTALLED_APPS

    ReplyDelete
  4. All the games you can play on the Sega Genesis - AprCasino
    The best part is, of course, the game, is that there are 출장마사지 very few people herzamanindir.com/ who do a lot apr casino of the hard 1등 사이트 work. The febcasino best part is, if the

    ReplyDelete