0

What is the proper way to say what is my AUTH_USER_MODEL?

I have the following set:

Folder structure:

--- backend
----- api
-------- models
----------- user.py

user.py lies within models folder

in settings.py:

AUTH_USER_MODEL = 'myapp.User'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'api',
]

The model:

class User:

    class Meta:
        app_label = "myapp"

And when I run any manage.py command, for example

python manage.py showmigrations

I get this error:

LookupError: No installed app with label 'myapp'.

The problem solution would be renaming api folder to myapp, which I cannot do due to some restrictions on model names.

Or setting AUTH_USER_MODEL to api.User, but this will incur changing all data table names and those must remain the same

Data table names starting with 'myapp_' should not change

Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46

2 Answers2

3

Given your folder structure looks like that:

backend
└── api
|   ├── __init__.py
|   ├── apps.py
|   └── models
|       ├── __init__.py
|       └── user.py
├── manage.py
└── settings.py

then you can create an app config in backend/api/ by creating a file called apps.py inside it. There you can rename your app:

backend/api/apps.py:

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    name = 'myapp'
    verbose_name = _('My App')

Also you need to add this to the __init__.py inside that folder:

backend/api/__init__.py:

default_app_config = 'api.apps.MyAppConfig'

Also if you want to use myapp.User as your user model you also have to import in in the models module:

backend/api/models/__init__.py:

from .user import User
# or use "from .user import *" to import everything but then make sure you have __all__ defined in user.py
trixn
  • 15,761
  • 2
  • 38
  • 55
  • Yes, exactly the same folder structure – Dmitry Dyachkov May 04 '18 at 17:05
  • ModuleNotFoundError: No module named 'myapp': apps.populate(settings.INSTALLED_APPS) -> app_config = AppConfig.create(entry) -> File "/Users/dmitri/Envs/chat3/lib/python3.6/site-packages/django/apps/config.py", line 142, in create app_module = import_module(app_name) – Dmitry Dyachkov May 04 '18 at 17:07
  • 1
    The solution was to change AppConfig.label in class MyAppConfig(AppConfig), so if you update your answer, I will accept it. Changing name ends in nothing, it must be the real name of the module the code sits in, but adding label = 'arbitrary_name' is actually the solution – Dmitry Dyachkov May 06 '18 at 07:44
  • I have anoter question related to `AUTH_USER_MODEL` but wasn't that big of a question to open a new question for it. Why don't we say `myApp.models.CustomUser` and say `myApp.CustomUser` instead? – Vahid Jan 14 '23 at 08:19
1

The solution was to change AppConfig.label in class MyAppConfig(AppConfig), so if you update your answer, I will accept it. Changing name ends in nothing, it must be the real name of the module the code sits in, but adding label = 'arbitrary_name' is actually the solution

Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46