-1

As in question 36046698, I have a CustomUser model defined in models.py.

I would like it to appear in the Authentication & Authorization section in Django Admin.

I tried adding app_label = 'auth' as suggested, i.e.

class CustomUser(AbstractUser):
    class Meta:
        app_label = 'auth'

When I do that, my app won’t start, and errors out like so:

Traceback (most recent call last):
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/config.py", line 178, in get_model
    return self.models[model_name.lower()]
KeyError: 'customuser'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 157, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/registry.py", line 211, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/config.py", line 180, in get_model
    raise LookupError(
LookupError: App 'myapp' doesn't have a 'CustomUser' model.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
....
  File "/Users/paul/myapp/myapp/admin.py", line 3, in <module>
    from django.contrib.auth.admin import UserAdmin
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/admin.py", line 6, in <module>
    from django.contrib.auth.forms import (
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 21, in <module>
    UserModel = get_user_model()
  File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 161, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'myapp.CustomUser' that has not been installed

In settings.py, I have

AUTH_USER_MODEL = 'myapp.CustomUser'

If I comment out that line, the server starts, but page loads fail with:

ValueError: Cannot create form field for 'user' yet, because its related model
'CustomUser' has not been loaded yet

This seems to be the same crash as question 62394664.

Paul Schreiber
  • 12,352
  • 4
  • 37
  • 62

1 Answers1

-1

It's seems like you forgot to migrate database. try with this command python manage.py makemigrations python manage.py migrate

  • Not correct. Why did you suggest that? My database is migrated. And adding `app_label = 'auth'` causes the same crash on startup if I try to run `makemigrations` or `migrate`. – Paul Schreiber Dec 22 '20 at 04:42
  • Then you should use model instead of app_label I mean model = 'auth' – Royal Patel Dec 22 '20 at 04:46
  • That results in a different error: `TypeError: 'class Meta' got invalid attribute(s): model` – Paul Schreiber Dec 22 '20 at 04:48
  • I would like to say that please refer https://docs.djangoproject.com/en/3.1/topics/auth/customizing/ you should have to use db_table as well otherwise it does not recognize that which model I have to use. – Royal Patel Dec 22 '20 at 05:10
  • So it would like app_label = 'auth' db_table='auth_user' – Royal Patel Dec 22 '20 at 05:17