Django cannot register oauth2_provider and rest_framework to INSTALLED_APPS

I am working on this weekend project, to learn Django and I am stuck.

Before adding the REST framework (one to last commit in the repo), everything was working just alright.

Once I added the djangorestframework library, everything fell apart. Now, whether you run the app on a venv or on DOcker, you will get the same result:

RuntimeError: Model class oauth2_provider.models.Application doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

... or a similar one related to rest_framework's Token. Both libraries are installed.

I understand the problem is that something is wrong with INSTALLED_APPS. But... WHAT?!

The docs make me think I am not doing anything wrong. If you look at the imports, from rest_framework etc etc and from oauth2_provider etc etc seem to be the problem.

Error logs and stack traces are quite useless, there is not useful info in there. Your help will be much appreciated.

It seems like you're missing a configuration in your settings.py. According to the docs, you need to tell Django REST Framework to use the new authentication backend (i.e. oauth2_provider) but you seem to be using TokenAuthentication.

To do so, comment out the TokenAuthentication and add the following lines to your settings.py:

REST_FRAMEWORK = {     'DEFAULT_AUTHENTICATION_CLASSES': [         'oauth2_provider.contrib.rest_framework.OAuth2Authentication',     ], }

Based on the error, it seems likely you missed a step in one of the external guides your tutorial links to:

Step 3: Configure Settings

Update settings.py:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'rest_framework.authtoken',
    'oauth2_provider',            # <-- This is what your error is about
    'social_auth',
    'accounts.apps.AccountsConfig',
]

I fixed it, although the error logs could not possibly more deceiving.

  • removed the following from settings.py, which I found here
import django
django.setup()
  • removed the folder `models`, and refactored according to the docs

  • created `admin.py` to register the CustomUser model (this I missed completely until now)

... and that's it. Once again, Python's lack of meaningful error logs and stack trace has made me waste hours.

Back to Top