django rest framework: type object 'Token' has no attribute 'objects'

Created:

The error

When you use the ObtainAuthToken to create an authentication token using rest framework you can get the following error (scroll down for solution):

Error
Traceback (most recent call last):
  File "/opt/project/user/tests/test_user_api.py", line 76, in test_create_token_for_user
    res = self.client.post(reverse('user:token'), payload)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/test.py", line 294, in post
    path, data=data, format=format, content_type=content_type, **extra)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/test.py", line 207, in post
    return self.generic('POST', path, data, content_type, **extra)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/test.py", line 232, in generic
    method, path, data, content_type, secure, **extra)
  File "/usr/local/lib/python3.7/site-packages/django/test/client.py", line 421, in generic
    return self.request(**r)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/test.py", line 283, in request
    return super().request(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/test.py", line 235, in request
    request = super().request(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/test/client.py", line 496, in request
    raise exc_value
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/authtoken/views.py", line 46, in post
    token, created = Token.objects.get_or_create(user=user)
AttributeError: type object 'Token' has no attribute 'objects'

Solution

Add 'rest_framework.authtoken' to INSTALLED_APPS in your settings.py configuration file.