3

After upgrading from Django 1.10 to 1.11, I've been receiving an issue when doing just about anything. I can't start my server, nor run manage.py.

Before upgrading, I did not receive this issue so I am not sure what's changed since then that would cause this issue.

I've also already tried deleting the database, nothing's changed.

My settings.py AUTH_USER_MODEL/INSTALLED_APPS:

AUTH_USER_MODEL = 'accounts.User'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
    'forum.apps.ForumConfig',
]

My User model:

class User(AbstractUser):
    profile_picture = models.ImageField(null=True, blank=True)
    birth_date = models.DateField(null=True, blank=True)

The error:

> Failed to get real commands on module "CsForum": python process died with code 
1: Traceback (most recent call last):
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 169, 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 "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py", line 193, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 205, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 172, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'accounts' doesn't have a 'User' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2017.2.1\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module>
    django.setup()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\Elian\OneDrive\PycharmProjects\CsForum\accounts\models.py", line 1, in <module>
    from django.contrib.auth.admin import UserAdmin
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\admin.py", line 7, in <module>
    from django.contrib.auth.forms import (
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\forms.py", line 22, in <module>
    UserModel = get_user_model()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py", line 198, in get_user_model
    "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'accounts.User' that has not been installed
Elian
  • 125
  • 1
  • 11

5 Answers5

7

I have managed to solve this one. The issue was that my CustomUserAdmin (extended UserAdmin for custom fields in the admin page) could no longer be in my models.py after upgrading to 1.11. It is now in my admin.py and the issue no longer occurs.

Elian
  • 125
  • 1
  • 11
1

Just register all your models in admin.py file. In your case --Admin.py--

admin.site.register(User)

It worked for me.

89f3a1c
  • 1,430
  • 1
  • 14
  • 24
0

I had a similar issue where I placed the CustomModelBackend in models.py

Placing it in a separate backends.py solved the issue

Dev Aggarwal
  • 7,627
  • 3
  • 38
  • 50
0

You should register your user model after installed apps. App should be initialize before models because models are in app.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
    'forum.apps.ForumConfig',
]
AUTH_USER_MODEL = 'accounts.User'
Arun
  • 3,440
  • 1
  • 11
  • 19
0

I had the same issue when I tried to create a Custom user and proxy models called my MyUser which used email address as the username. The best practice to avoid this error is to create your custom user before running any of the migrations The solution for me was to:

  • Reset all migrations

Go to your projects migrations folder and delete everything inside expect __init__.py. On Linux or GitBash run:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
  • Drop your database or delete db.sqlite3 if that's what you're using.

If you had text information like users information already in your db.sqlite3 database and you want to have if before you drop the database. Create a file backup.py in the project directory (don't worry delete it after running it) and paste in the following code to backup your text information.

import sqlite3
connection = sqlite3.connect('db.sqlite3')  # or the name of your sqlite db
cursor = connection.cursor()

app_name = "your_app" # replace with the name of your app
tables = []

for table_tuple in cursor.execute("SELECT name FROM sqlite_master WHERE type='table'"):
  for table_name in table_tuple:
    if app_name in table_name:
      tables.append(table_name)

data = ''
for table in tables:
  rows = ''
  for row in cursor.execute("SELECT * FROM %s" %table):
    rows += "\t%s,\n"%str(row)
  data += '\n%s=[\n%s\n]\n'%(table,rows[0:-2])

with open('backup.txt', 'w') as my_file:
  my_file.write(data)

Run the file and there will be another file backup.txt where your data will reside.

  • Create migrations again and generate the database schema.

In our project directory as usual run:

python manage.py makemigrations
python manage.py migrate

For now, your problem should be solved and you can start adding your data again. For SQLite users, you can lookout the syntax of adding data into the database but running:

cursor.executemany ("INSERT INTO your_database_table_name VALUES (?,?,?,?)", data)

The (?,?,?,?) is the number of columns in your table. If you get it wrong - no problem the database will give you a hint on the number of rows you need to have.

Essentially you can copy the each of the data we saved from the last backup.txt file in the above script with correct adjustments.

samnodier
  • 355
  • 3
  • 2