ValueError in Django when running the “python manage.py migrate” command

余生颓废 提交于 2019-12-05 18:22:25

问题


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'.

回答1:


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




回答2:


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.




回答3:


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




回答4:


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




回答5:


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".




回答6:


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.



来源:https://stackoverflow.com/questions/40222268/valueerror-in-django-when-running-the-python-manage-py-migrate-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!