encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.45k stars 6.84k forks source link

type object 'Token' has no attribute 'objects' #6250

Closed doctorallen closed 6 years ago

doctorallen commented 6 years ago

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 "<console>", line 1, in <module>
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?

xordoquy commented 6 years ago

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).

doctorallen commented 6 years ago

@xordoquy Thank you!

aseem-hegshetye commented 4 years ago

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

tinshade commented 2 years ago

Add 'rest_framework.authtoken' to INSTALLED_APPS list in settings.py Don't forget to run a python manage.py migrate after making changes in your settings file

nickmccullum commented 2 years ago

This comment solved my problem, 4 years later. Thanks!

HRUAS commented 1 year ago

thank you @xordoquy

Lokesh-Chigulla commented 1 year ago

I have tried all the steps above but still this error is popping TypeError at /login/ Object of type method is not JSON serializable

GunzzGT commented 1 year ago

still returns rest_framework.authtoken.models.Token.DoesNotExist: Token matching query does not exist. after adding authtoken in 'INSTALLED_APPS'

traceback shows self.token = Token.objects.get(user=self.user) is that a bad way to get token.objects?

vishal-ahirwar commented 1 year ago

rest_framework.authtoken

thank you :)

siecp-1001 commented 6 months ago

thanks you

IxBahy commented 5 months ago

still returns rest_framework.authtoken.models.Token.DoesNotExist: Token matching query does not exist. after adding authtoken in 'INSTALLED_APPS'

traceback shows self.token = Token.objects.get(user=self.user) is that a bad way to get token.objects?

did u find a way to solve this I'm using a custom user and I guess its the problem :/