Forums

Need Help ## AttributeError: type object 'Token' has no attribute 'objects'

Hello,

I am using Django Rest framework and token based authentication. I hosted the webapp in Pythonanywhere website. When i try to login (using rest api call), Token generation is failing. Following is error details from pythonanywhere error log file

2016-07-01 13:25:42,249 : new_token = Token.objects.create(user=user) 2016-07-01 13:25:42,250 :AttributeError: type object 'Token' has no attribute 'objects'

I checked following things 1) Database has authtoken_token table

2) INSTALLED APPS has

'rest_framework', 'rest_framework.authtoken',

3) Settings has REST_FRAMEWORK = { # Use Django's standard django.contrib.auth permissions, # or allow read-only access for unauthenticated users.

'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.AllowAny',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PARSER_CLASSES': (
    'rest_framework.parsers.JSONParser',
)

}

This is working in my local system. I tried makemigrations and migrate command. It seems fine.

Please let me know if i am missing anything here.

Thanks in advance.

Are you using the same version of Django and Django Rest Framework on your local system as you are on PythonAnywhere?

Yes. i am using same version on local system and PythonAnyhwere

Django (1.9.6) django-filter (0.13.0) djangorestframework (3.3.3)

Just to confirm, you did the migrate on the pythonanywhere system as well, and you had reloaded the webapp afterwards right?

Yes. I did migrate and reloaded the webapp many times. I connected to db console and checked that table authtoken_token is created. I am trying to login/authenticate using rest api and token creation is failing.

Another dumb thing to double check (sorry if this is obvious etc)- are you importing Token like this? from rest_framework.authtoken.models import Token

And if you do a pip freeze inside of your virtualenv, you do get the correct django/drf/django-filter versions right?

I am importing token using from from rest_framework.authtoken.models import Token

Following is output of pip freeze in pythonanywhere

Django==1.9.6 django-filter==0.13.0 djangorestframework==3.3.3 Markdown==2.6.6 mysqlclient==1.3.7 Pillow==3.2.0

This matches to the versions in my local system.

In my code i have following lines print (type(Token)) # This prints <class 'django.db.models.base.ModelBase'> new_token = Token.objects.get_or_create(user=user) # This gives error AttributeError: type object 'Token' has no attribute 'objects'

Please let me know if i can provide any details. Thanks.

The problem is resolved. I have multile settings files. One for testing in local system and one for testing in pythonanywhere.com. The local settings.py is up to date but the settings used in pythonanywhere is not updated. This caused the issue.

Thanks for your time and support.

This is my first Django app and new to web hosting. Is there any check list or guidelines for deploying Django app in different environments (dev, prod etc) and organizing environment dependent files. Please suggest.

The suggestions on this Stack Overflow post are really good. One difference for PythonAnywhere is that where they refer to using the --settings option when running Django to specify which settings file you should use, instead you should change the settings file specified in the WSGI file (linked from the "Web" tab).

I am also getting error. I generate my token with '/auth-token-api' and use requests to generate it, it works on local but on pythonanywhere it keeps on loading and then crash , please help

What do you see in your logs? There are links to them on the "Web" page on Pythonanywhere.