简体   繁体   English

Django:在模型导入的上下文中没有名为“foo”的模块问题

[英]Django: No Module named 'foo' issue in context with model import

Background information:背景资料:
I would like to run the feeder.py script using atoms script plug-in.我想使用 atom script插件运行feeder.py脚本。 I first encountered a ImproperlyConfigured error which was solved as suggested here: First fix第一次遇到了一个ImproperlyConfigured错误,该错误已按照此处的建议解决: 首先修复

Then I run into RuntimeError: Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.然后我遇到了RuntimeError: Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. error which was solved by using an absolute path for the model import as shown below.错误是通过使用模型导入的绝对路径解决的,如下所示。

Current issue:当前的问题:
Using the mentioned absolute import, I receive this error:使用提到的绝对导入,我收到此错误:

ModuleNotFoundError: No module named 'Dashboard_app'

I can even render the template for that app etc. so I am confused why he tells me that the module doesn't exist.我什至可以为该应用程序等渲染模板,所以我很困惑为什么他告诉我该模块不存在。 When I remove the model import, everything works just fine.当我删除模型导入时,一切正常。 Is it maybe that the script instance doesn't recognize it properly?是不是script实例不能正确识别它?

What I've tried:我试过的:

  • deleted + re-created __init__.py删除 + 重新创建__init__.py
  • checked settings for app to be included in INSTALLED-APPS dic检查要包含在INSTALLED-APPS dic 中的应用程序设置
  • changed import path to DASHEX.Dashboard_app.models resulting in no module named DASHEX error将导入路径更改为DASHEX.Dashboard_app.models导致no module named DASHEX错误
  • changed import path to models resulting in Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS error更改了models导入路径,导致Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS错误Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
  • changed the app in INSTALLED_APPS to Dashboard_appINSTALLED_APPS的应用程序更改为Dashboard_app

feeder.py script: 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:设置.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:模型.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

Project structure:项目结构:

在此处输入图片说明

I think (almost sure!) that the problem is about the settings.configure() ;我认为(几乎可以肯定!)问题出在settings.configure() as you didn't specify any default_settings , django will use it's default settings template (the one you see when you create a new project) and your Dashboard_app is not there.由于您没有指定任何default_settings ,django 将使用它的默认设置模板(您在创建新项目时看到的模板)并且您的Dashboard_app不在那里。 That's the cause of this error:这就是这个错误的原因:

changed import path to models resulting in Model class models.AccountInformation doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS error更改了模型的导入路径,导致模型类模型。AccountInformation 未声明显式 app_label 并且不在 INSTALLED_APPS 错误中的应用程序中

Try specifiying your settings inside the configure :尝试在configure指定您的设置:

from dashex import settings

settings.configure(settings)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM