Django 报错如下
RuntimeError: Model class apps.alarms.models.SendAlarmRecord doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
项目将所以的APP都放在在apps下,并且在项目的settings.py
中添加了相关配置:
from pathlib import Path
import sys
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
APP_DIR = os.path.join(BASE_DIR, "apps")
sys.path.insert(0, APP_DIR)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'apps.alarm.apps.AlarmConfig',
]
在view.py
中使用以下两种方式导入包时都报错,
from .models import SendAlarmRecord
from apps.alarm.models import SendAlarmRecord
这个报错的处理是在apps.alarm.apps
文件中修改如下:
from django.apps import AppConfig
class AlarmConfig(AppConfig):
# 改为apps.app名称
name = 'apps.alarm'