Django-rest-framework: type object 'Token' has no attribute 'objects'

Created on 16 Oct 2018  ·  3Comments  ·  Source: encode/django-rest-framework

I'm trying to implement my own serializer and view to handle Token based authentication with email instead of username. In copying the ObtainAuthToken view, an error is returned about the Token object not having the objects attribute.

Steps to reproduce

  1. mkdir restframework
  2. cd restframework/
  3. virtualenv env
  4. source env/bin/activate
  5. pip install django
  6. pip install djangorestframework
  7. django-admin startproject tutorial
  8. cd tutorial
  9. python manage.py shell
    ```
    from rest_framework.authtoken.models import Token
    Token.objects.all()
## Expected behavior
Token class can query objects

## Actual behavior

python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

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

This class (being a django model) should be able to query using the objects attribute, should it not?

Most helpful comment

That's because you didn't add the auth token in the settings' INSTALLED_APPS

Edit: if it's not the in the INSTALLED_APPS, it's abstract and doesn't have the default manager (objects).

All 3 comments

That's because you didn't add the auth token in the settings' INSTALLED_APPS

Edit: if it's not the in the INSTALLED_APPS, it's abstract and doesn't have the default manager (objects).

@xordoquy Thank you!

Add 'rest_framework.authtoken' to INSTALLED_APPS list in settings.py

Was this page helpful?
0 / 5 - 0 ratings