简体   繁体   中英

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. I first encountered a ImproperlyConfigured error which was solved as suggested here: First fix

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. 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?

What I've tried:

  • deleted + re-created __init__.py
  • checked settings for app to be included in INSTALLED-APPS dic
  • changed import path to DASHEX.Dashboard_app.models resulting in no module named DASHEX 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
  • changed the app in INSTALLED_APPS to Dashboard_app

feeder.py script:

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

Project structure:

在此处输入图片说明

I think (almost sure!) that the problem is about the 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. 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

Try specifiying your settings inside the configure :

from dashex import settings

settings.configure(settings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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