10

I needed to add more fields to Django's User model, so I created a custom model class (named Accounts in an app named accounts) that extends Django's AbstractUser class.

After that, I updated my settings.py file, defining the AUTH_USER_MODEL property:

AUTH_USER_MODEL = 'accounts.Accounts'

I then created a migration file for the custom model using the python manage.py makemigrations command.

After that, I ran the python manage.py migrate command and I got this error message:

ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' isn't installed.

What's the cause of the error and how can I fix it?

UPDATE: Now, if i run the python manage.py makemigrations command, I get this error message:

ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' doesn't provide model 'accounts'.
Inyavic Sage
  • 145
  • 1
  • 2
  • 11
  • Do you have the `accounts` package in `INSTALLED_APPS`? – koniiiik Oct 24 '16 at 15:36
  • @koniiiik, this is what I have in `INSTALLED_APPS`: INSTALLED_APPS = [ 'accounts.apps.AccountsConfig', 'inyavic.apps.InyavicConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] – Inyavic Sage Oct 24 '16 at 15:47
  • 1
    In that case, I'm afraid we'll have to see the full traceback to be able to help further. Just add it to your question (you might also list the `INSTALLED_APPS` there to make it easier to read). – koniiiik Oct 25 '16 at 08:09

6 Answers6

14

You just delete your previous 0001_initial.py in the migrations folder and try doing the makemigration and migrate again

  • 1
    The solution is to do the migrations yourself. Simply deleting the migration and recreating migrations will leave the cruft that was the original user model in your db - i.e., bad for a production system. So figure out what's acceptable. If you don't need any of your current data, then totally nuke your db and migrations and rebuild from scratch. If you're still at migration 0001, then this might be okay. But if you need to preserve data, then figure out how to do it manually. We can't know from this post if, say, there are any `User` rows that need preserved in `Accounts`. – TheGrimmScientist Jun 21 '18 at 16:28
  • tldr: this seems to be rooted in that changing the core user model breaks the migration system. So you need to understand what the migration would have ideally done for you and do it manually. – TheGrimmScientist Jun 21 '18 at 16:29
  • To add to this, be aware that if you're making model changes in different apps, and deleting migrations to try and makemigrations again, there might've been migration files created in both apps. I got the error because I deleted 0001_initial.py in app1/migrations but didn't delete 0121_somemigrations.py from app2/migrations. Then I ran "python manage.py makemigrations" and got this error. After I deleted 0121_somemigrations.py, everything worked again. – Darren Alfonso Feb 16 '19 at 18:50
2

I have the similar problem. It is the admin app has the cache and migrations history. I solve it by deleting all the cache and migrations history record(pycache file, and 0001.intial etc., keep init.py only) in YouProject\lib\site-packages\django\contrib\admin\migrations

Henning Lee
  • 544
  • 4
  • 13
2

I too had a similar problem when I changed the name of one of my apps, I had to delete migrations files at two locations, all migrations for the specific app migration folder, then migrations at "Your-project-env/lib/python3.5/site-packages/django/contrib/admin/migrations".

Aldo Okware
  • 146
  • 1
  • 4
1

You didn't add accounts to your INSTALLED_APPS. From the comment, I can see accounts.apps.AccountsConfig in your list of apps. Instead of it, just add accounts to your INSTALLED_APPS

Adilet Maratov
  • 1,312
  • 2
  • 14
  • 24
1

It's just because you have already an instance of default user model I think. Start a new project and migrate your models again and it should work.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Mahdi Sorkhmiri
  • 356
  • 2
  • 4
  • 16
  • How can I start a new project when I get this issue somewhere middle of my project where everything is working fine since 2 years!! – Shashank Hegde Jun 01 '20 at 16:31
0

I have the same problem. look under your class in models.py, change the app_label to the name of your application, I put the name of the class, and then the error came. doing this I think it fixes the errors.

class Meta:
        app_label = 'put_app_name'

Sorry for some grammar error, I'm using google translator.