【问题标题】:Django: No Module named 'foo' issue in context with model importDjango:在模型导入的上下文中没有名为“foo”的模块问题
【发布时间】:2019-12-03 12:16:12
【问题描述】:

背景信息:
我想使用 atom script 插件运行 feeder.py 脚本。我首先遇到了ImproperlyConfigured 错误,已按照此处的建议解决:First fix

然后我遇到RuntimeError: Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. 错误,该错误通过使用模型导入的绝对路径解决,如下所示。

当前问题:
使用提到的绝对导入,我收到此错误:

ModuleNotFoundError: No module named 'Dashboard_app'

我什至可以为该应用程序渲染模板等,所以我很困惑他为什么告诉我该模块不存在。当我删除模型导入时,一切正常。是不是 script 实例无法正确识别?

我的尝试:

  • 已删除 + 重新创建 __init__.py
  • 检查了应用程序的设置以包含在INSTALLED-APPS dic 中
  • 将导入路径更改为DASHEX.Dashboard_app.models 导致no module named DASHEX 错误
  • 将导入路径更改为models 导致Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS 错误
  • INSTALLED_APPS 中的应用更改为Dashboard_app

feeder.py 脚本:

import django
from django.conf import settings
import zmq
import time
from time import sleep
import uuid
settings.configure()
django.setup()
import sys
print(sys.path)
from Dashboard_app.models import AccountInformation
[...]

settings.py:

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    #other Apps
    'Wiki_app',
    'rest_framework',
    'Dashboard_app.apps.DashboardAppConfig'
]

Models.py:

from django.db import models
import uuid

# Create your models here.


class AccountInformation(models.Model):
    version = models.CharField(max_length=20, blank=False)
    DID = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    accountNumber = models.IntegerField(blank=False)
    broker = models.CharField(max_length=50, blank=False)
    leverage = models.CharField(max_length=10, blank=False)
    account_balance = models.FloatField(max_length=15, blank=False)
    account_profit = models.FloatField(max_length=15, blank=False)
    account_equity = models.FloatField(max_length=15, blank=False)
    account_margin = models.FloatField(max_length=15, blank=False)
    account_margin_free = models.FloatField(max_length=15, blank=False)
    account_margin_leve = models.FloatField(max_length=15, blank=False)
    account_currency = models.CharField(max_length=20, blank=False)

    class Meta:
        db_table = 'AccountInfo'

    def __str__(self):
        return self.accountNumber

项目结构:

【问题讨论】:

    标签: python django


    【解决方案1】:

    我认为(几乎可以肯定!)问题在于settings.configure();由于您没有指定任何default_settings,django 将使用它的 default 设置模板(您在创建新项目时看到的模板)并且您的Dashboard_app 不存在。这就是这个错误的原因:

    更改了模型的导入路径,导致模型类 models.AccountInformation 未声明显式 app_label 并且不在应用程序中出现 INSTALLED_APPS 错误

    尝试在configure 中指定您的设置:

    from dashex import settings
    
    settings.configure(settings)
    

    【讨论】:

    • 就是这样。你是神!
    猜你喜欢
    • 2012-10-26
    • 2020-03-28
    • 1970-01-01
    • 2013-07-17
    • 2017-03-13
    • 1970-01-01
    • 2012-03-16
    • 2016-01-19
    相关资源
    最近更新 更多