django.db.utils.ProgrammingError: (1146, "Table 'db_gold.user_ip_info' doesn't exist") RuntimeError: Model class scanhosts.models.HostLoginInfo doesn't declare an explicit app_label and isn't in an a

时间:2023-03-08 23:58:23
django.db.utils.ProgrammingError: (1146, "Table 'db_gold.user_ip_info' doesn't exist")  RuntimeError: Model class scanhosts.models.HostLoginInfo doesn't declare an explicit app_label and isn't in an a

Error Msg

创建了一个apps的目录将所有app放入apps文件中, 将apps路径加入sys.path中:
sys.insert(0, os.path.join(BASE_DIR, "apps")) 未改前:
RuntimeError: Model class scanhosts.models.HostLoginInfo doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. 安装app到install_apps中后, 变成:
django.db.utils.ProgrammingError: (1146, "Table 'db_gold.user_ip_info' doesn't exist")

原因及解决:

  1. 没有安装apps, 在setting.py中安装app

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"users",
"scanhosts",
"details"
]

  2. 安装app后, 所有用到app中的模块导入必须使用app.模块名,  不能使用上级目录apps

只能使用:
from scanhosts.utils import login_ssh_do
from scanhosts.models import *
不能使用:
from apps.scanhosts.utils import login_ssh_do
from apps.scanhosts.models import *

  3. 如果无法迁移, 可指定迁移model来迁移:

python manage.py makemigrations users scanhosts details
python manage.py migrate