The following error is thrown after setting AUTH_USER_MODEL = "bsapi.core.User"
:
ValueError: Invalid model reference 'bsapi.core.User'. String model references must be of the form 'app_label.ModelName'.
- The APP is registered like:
INSTALLED_APPS = [
...
"bsapi.core.apps.CoreConfig",
"second_app.apps.SecondAppConfig",
...
]
- The core APP config is as
class CoreConfig(AppConfig):
name = "bsapi.core"
- The structure of the project is
- bsapi
- core
- apps.py
- second_app
- settings.py
- apps.py
How can I set AUTH_USER_MODEL
for an app where the name has “.”.
Where is that user model defined?
Briefly, django.apps.config.AppConfig.get_model is looking in the “apps registry” for that model. So you don’t specify the model using an import path, it’s specified in AUTH_USER_MODEL using the app.model name convention.
The User model is under bsapi.core.models.py
- bsapi
- core
- apps.py
- models.py
- second_app
- settings.py
- apps.py
Ok, I should have been more clear in my previous response.
The models are stored by app label, not by app name. (See Applications | Django documentation | Django)
What this means is that you can either define a label for your app in your AppConfig, or assuming there’s no conflict with any other apps, use the default, which will be core
.
e.g. AUTH_USER_MODEL = "core.User"
Changed the app label to core1 in AppConfig:
class CoreConfig(AppConfig):
name = "bsapi.core"
label = "core"
And changed the AUTH_USER_MODEL to AUTH_USER_MODEL = "core.User"
, and now I’m getting the following error:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'core.User' that has not been installed
Double-check your settings file to make sure that you don’t have either two lines for AUTH_USER_MODEL in it, or that you don’t have a typo for that line.
Don’t know if the following info bellow is relevant:
(Pdb) settings.AUTH_USER_MODEL
'core.User'
(Pdb) django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
*** LookupError: App 'core' doesn't have a 'User' model.
(Pdb) django_apps.is_installed("core")
False
(Pdb) django_apps.is_installed("bsapi.core")
True
(Pdb)
Looks like the app has no models at the moment the import_module is called
(Pdb) django_apps.all_models
defaultdict(<class 'dict'>, {'runserver_nostatic': {}, 'admin': {'logentry': <class 'django.contrib.admin.models.LogEntry'>}, 'contenttypes': {'contenttype': <class 'django.contrib.contenttypes.models.ContentType'>}, 'core': {}, 'auth': {'permission': <class 'django.contrib.auth.models.Permission'>, 'group_permissions': <class 'django.contrib.auth.models.Group_permissions'>, 'group': <class 'django.contrib.auth.models.Group'>, 'user': <class 'django.contrib.auth.models.User'>}, 'sessions': {'session': <class 'django.contrib.sessions.models.Session'>}, 'messages': {}, 'staticfiles': {}, 'django_cleanup': {}, 'anymail': {}, 'rest_framework': {}, 'authtoken': {'token': <class 'rest_framework.authtoken.models.Token'>, 'tokenproxy': <class 'rest_framework.authtoken.models.TokenProxy'>}, 'zizoo': {}})
As you can see, core is empty. Does this help?
Hmmm,… I wouldn’t think it would matter - my understanding is that that’s the purpose behind the require_ready=False
parameter. From those docs:
Requires the app registry to be fully populated unless the require_ready
argument is set to False
Those docs also refer to Applications | Django documentation | Django, which includes:
Setting require_ready
to False
allows looking up models while the app registry is being populated, specifically during the second phase where it imports models. …
The main use case is to configure model classes with settings, such as AUTH_USER_MODEL
.
I freely admin, my knowledge of the Django initialization process is at best, sparse.
I would not be surprised to find out the AUTH_USER_MODEL just isn’t going to work in a model not directly in a “root” app directory. (I’ve never worked with, nor ever encountered a need or desire to try and segregate code like that.)