[Django]-Django LookupError: App 'accounts' doesn't have a 'User' model

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

1👍

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

admin.site.register(User)

It worked for me.

0👍

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

Placing it in a separate backends.py solved the issue

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

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.

Leave a comment