migrations时候报错

来源:7-3 自定义userprofile表覆盖默认的user表

Essence_CC

2021-03-22

已经按照视频中讲到的在settings中加上了
AUTH_USER_MODEL = "users.UserProfile"
但还是在迁移数据库文件时报错 ,不知道是哪里的问题
Traceback (most recent call last):
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\config.py”, line 178, in get_
model
return self.models[model_name.lower()]
KeyError: ‘userprofile’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth_init_.py”, line 16
5, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py”, line 210, in ge
t_model
return app_config.get_model(model_name, require_ready=require_ready)
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\config.py”, line 181, in get_
model
"App ‘%s’ doesn’t have a ‘%s’ model." % (self.label, model_name))
LookupError: App ‘users’ doesn’t have a ‘UserProfile’ model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “manage.py”, line 21, in
main()
File “manage.py”, line 17, in main
execute_from_command_line(sys.argv)
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management_init_.py”, line
381, in execute_from_command_line
utility.execute()
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management_init_.py”, line
357, in execute
django.setup()
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django_init_.py”, line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py”, line 122, in po
pulate
app_config.ready()
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\apps.py”, line 24, i
n ready
self.module.autodiscover()
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin_init_.py”, line 2
6, in autodiscover
autodiscover_modules(‘admin’, register_to=site)
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\module_loading.py”, line 47,
in autodiscover_modules
import_module(’%s.%s’ % (app_config.name, module_to_search))
File “C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\importlib_init_.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “

  1. userprofile表定义源码

http://img.mukewang.com/szimg/60594a7e09a22ce517040494.jpg

    2. 左侧代码导航结构

http://img.mukewang.com/szimg/60594a9609aecbf905860633.jpg

    3. users下的apps.py源码

http://img.mukewang.com/szimg/60594aab097b8dbe10600397.jpg

    4. install_apps的配置

http://img.mukewang.com/szimg/60594ac10950b05e07400415.jpg


写回答

2回答

bobby

2021-03-23

将下面的内容截图我看看:

    1. userprofile表定义源码

    2. 左侧代码导航结构

    3. users下的apps.py源码

    4. install_apps的配置

0
4
bobby
回复
Essence_CC
好的。
2021-03-23
共4条回复

Essence_CC

提问者

2021-03-23

今天重新试了一下,makemigrations成功创建了UserProfile,但是migrate继续报错 下面是报错内容

E:\djangoProject\myweb>python manage.py migrate

Operations to perform:

  Apply all migrations: admin, auth, contenttypes, poem, sessions, users

Traceback (most recent call last):

  File "manage.py", line 21, in <module>

    main()

  File "manage.py", line 17, in main

    execute_from_command_line(sys.argv)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line

 381, in execute_from_command_line

    utility.execute()

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line

 375, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 323

, in run_from_argv

    self.execute(*args, **cmd_options)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 364

, in execute

    output = self.handle(*args, **options)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 83,

 in wrapped

    res = handle_func(*args, **kwargs)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\migrate.p

y", line 191, in handle

    pre_migrate_apps = pre_migrate_state.apps

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 80, in

__get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 210,

 in apps

    return StateApps(self.real_apps, self.models)

  File "C:\Users\华硕\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 280,

 in __init__

    raise ValueError("\n".join(error.msg for error in errors))

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

't installed.


0
1
Essence_CC
在网页上查到下面这个解决方法 migrate后正常了 ---------------------------------------------------------------------- 进入site-packages/django/contrib/admin/migrations文件目录下,除了__init__.py文件,其他的全部删除。
2021-03-23
共1条回复

Django+ xadmin 开发在线教育网站

一套通用的技术组合拳,助你解决大部分Python类网站后端问题

1911 学习 · 2149 问题

查看课程