Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LookupError: App 'users' doesn't have a 'user' model

Tags:

django

Django 1.9.7 I'm using pyenv virtualenv autoenv

I want to extend user model so, i decide to use AbstractUser

(AbstractUser's class META abstract = True, so i can't make table, but inheritance class can make table, right??)

anyways

(wef is project name) I make app wef/users/models/__init__.py

from .user import User

In wef/users/models/user.py

from django.contrib.auth.models import AbstractUser

from django.db import models


class User(AbstractUser):

    phonenumber = models.CharField(
            max_length = 11,
            blank = True,
            null = True
            )

and I add usersapp in settings.py

INSTALLED_APPS = [
    [...]
    'users',
]

AUTH_USER_MODEL = 'users.User'

So, I think when i makemigrations, migrate

django will make model table about User...

python wef/manage.py makemigrations users

it shows error

Traceback (most recent call last):
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 163, in get_model
return self.models[model_name.lower()]
KeyError: 'user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 398, in execute
self.check()
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model
cls = apps.get_model(settings.AUTH_USER_MODEL)
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/registry.py", line 197, in get_model
return self.get_app_config(app_label).get_model(model_name.lower())
  File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 166, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'users' doesn't have a 'user' model.

I can't understand why django can't found users.User model

and when I change `AUTH_USER_MODEL=UserAAA'

it show error (capital letters is changed lower case)

LookupError: App 'users' doesn't have a 'useraaa' model.

I can not find my problem please somebody help me.. ㅠ_ㅠ

like image 628
Jade Han Avatar asked Dec 24 '22 03:12

Jade Han


2 Answers

I think you have already created your database schema. From the Django documentation:

Changing AUTH_USER_MODEL has a big effect on your database structure. It changes the tables that are available, and it will affect the construction of foreign keys and many-to-many relationships. If you intend to set AUTH_USER_MODEL, you should set it before creating any migrations or running manage.py migrate for the first time.

Changing this setting after you have tables created is not supported by makemigrations and will result in you having to manually fix your schema, port your data from the old user table, and possibly manually reapply some migrations.

like image 169
Ankur Gupta Avatar answered Feb 11 '23 22:02

Ankur Gupta


I have a couple more options to resolve this.

  1. If you really want to migrate from the the contrib.auth

Put db_table = 'auth_user' in your model's Meta to use the existing table and not break the other relations. Make a migration without any custom fields, then make your model changes and another migration.

  1. I moved my models.py to models/init.py

In that case I needed to have from .user import User before importing any other models that called get_user_model()

like image 34
Aaron McMillin Avatar answered Feb 11 '23 23:02

Aaron McMillin