【问题标题】:RuntimeError: Model class xxx.xxx doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPSRuntimeError:模型类 xxx.xxx 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用程序中
【发布时间】:2018-12-29 18:31:01
【问题描述】:

尝试运行 django 2 应用程序时出现以下错误

stacktrace 中的几行

文件 "/Users/shobi/Projects/emailtool/emailtool/frontend/models/AwsSettings.py", 第 4 行,在

从 emailtool.frontend.models.AwsRegions 导入 AwsRegions 文件“/Users/shobi/Projects/emailtool/emailtool/frontend/models/AwsRegions.py”, 第 4 行,在

类 AwsRegions(models.Model):文件“/Users/shobi/Projects/emailtool/env/lib/python3.6/site-packages/django/db/models/base.py”,

第 108 行,

“INSTALLED_APPS”。 % (module, name) RuntimeError: Model class emailtool.frontend.models.AwsRegions.AwsRegions 没有声明 显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中。

我试过了

Django: Model class user.models.Users doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

还有很多谷歌搜索结果,缺少什么?

Django2,Python 3.6

编辑:

INSTALLED_APPS = [
    'frontend',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

【问题讨论】:

  • 你的INSTALLED_APPS是什么样的?
  • 查看 Django 教程。您必须将您的应用程序放在 settings.py 中
  • 您是否在该应用的 urls.py 中添加了 app_name = 'app_name'
  • @PankajSharma,不,我没有添加

标签: python django


【解决方案1】:

在尝试了所有可能的 Stackoverflow 答案后,我想通了。

在我的一些视图/表单中,我需要一个名为 AwsRegions 的模型,最初所有模型都在一个文件中,该文件后来移动到一个名为 models/ 的单独文件夹中,我更新了另一个需要此类的现有代码。

一段时间后,在尝试runserver 命令时出现了问题中指定的问题。 (我想 pycache 之前提供的是旧文件,所以它没有抛出任何错误。)。

但是这个错误根本没有任何意义。

然后我尝试运行python3 manage.py makemigrations 命令,它开始抛出错误,然后我修复了这些错误,这些错误是由于路径无效而导致的简单文件未找到错误。之后我尝试了python3 manage.py runserver,它运行成功了。

所以,本质上

  1. 运行python3 manage.py makemigrations
  2. 修复传入的错误
  3. 再次运行python3 manage.py makemigrations

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2016-05-25
    • 2020-05-16
    • 2017-09-05
    • 2023-02-18
    • 2020-08-13
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多