4

I've got an auth application which is used instead of django's own auth app, called authentication. Typically projects will specify AUTH_USER_MODEL = 'authentication.User'.

There are some other projects which utilise a separate authentication backend and an API which brings in additional fields/methods to the User model.

Because this additional functionality isn't used by all projects I'd like it to be an optional sub-application to authentication. Therefore I've added it to the package setup as an extra;

setup(
    name='authentication',
    extras_require={
        "api_app": ["api_client>=3.0.1"],
    },

api_app is then added as a sub-application to authentication:

- authentication
-- apps.py
-- models.py
-- api_app
--- apps.py
--- models.py

With this I've got two models for users, User in authentication.models and APIUser in authentication.api_app.models. For projects making use of APIUser I've got the following settings;


# AUTH_USER_MODEL = 'authentication.User'
AUTH_USER_MODEL = 'authentication_api.APIUser'  # Needs to be `app_label.Model`


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.humanize',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',

    'authentication',
    'authentication.api_app',
    'api_client'
]

api_app.__init__

default_app_config = 'authentication.api_app.apps.APIConfig'

The api_app is defined as;

class APIConfig(AppConfig):
    label = 'authentication_api'  # in an attempt to support `app_label.Model` ref in settings
    name = 'authentication.api_app'
    verbose_name = "API Authentication"

With this configuration django thinks that authentication_api isn't installed, but AUTH_USER_MODEL only supports app_label.Model and nothing more complex, so how can I set this application up to support the top level model or the sub-level model for AUTH_USER_MODEL?

Error I'm getting with the above setup;

django.core.exceptions.ImproperlyConfigured: 
AUTH_USER_MODEL refers to model 'authentication_api.APIUser' that has not been installed
markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • 1
    Inside your `api_app` folder, do you have an `__init__.py` file which sets [`default_app_config`](https://docs.djangoproject.com/en/3.0/ref/applications/#for-application-authors)? If not, django isn't reading your custom `AppConfig`. You can also tell your users to add `'authentication.api_app.apps.APIConfig'` as installed app. – dirkgroten Feb 10 '20 at 13:25
  • 1
    Also the correct attribute for the app label is `label` not `app_label`. – dirkgroten Feb 10 '20 at 13:29
  • @dirkgroten yeah the `AppConfig` is loaded in the init, I'll make a note of that in my question. Thanks for the note on `label` vs `app_label`, looks like I was getting mixed up with model definitions!! – markwalker_ Feb 10 '20 at 14:51

0 Answers0