Django: RuntimeError: Model class test_django.apps.app1.models.xxx doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
1.Django项目错误提示:
RuntimeError: Model class xxx.apps.users.models.User doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
2. 项目结构:
django_project ├── LICENSE ├── main.py └── test_django ├── manage.py └── test_django ├── apps │ ├── app1 │ │ ├── admin.py │ │ ├── apps.py │ │ ├── __init__.py │ │ ├── models.py │ │ ├── tests.py │ │ ├── urls.py │ │ └── views.py │ └── app2 │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ └── views.py ├── asgi.py ├── __init__.py ├── settings │ ├── dev.py │ └── prod.py ├── urls.py └── wsgi.py
2.解决
1. app内,使用相对路径引用Models模块:
# 在apps.app1.urls.py中导入apps.app1.views.py from . import views # 在apps.app1.views.py中导入apps.app1.models.py中的xxx from .models import xxx
2. 不同app之间,使用以apps为根目录的绝对路径:
# 在apps.app2.views.py中导入apps.app1.models.py中的xxx from app1.models import xxx