【问题标题】:I have "Requested setting INSTALLED_APPS" warnings我有“请求设置 INSTALLED_APPS”警告
【发布时间】:2018-08-27 12:25:59
【问题描述】:

我正在尝试从 Django 中的 BaseCommand 类扩展。但是,在运行 $ python manage.py runserver 时,我收到以下错误:

Requested setting INSTALLED_APPS

我的settings.py

 INSTALLED_APPS = [
    'suit',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "aksalist",
]

当我运行test_et.py 时,出现此错误。

test_et.py:

from django.core.management.base import BaseCommand
from aksalist.models import *

class Command(BaseCommand):
    help = 'Get single products from cs-cart'

    def handle(self, *args, **options):
        pass

bolge = Bolgeler.objects.all()
personel = Aksalist.objects.all()[0]

gunler = ("2018-08-28", "2018-08-29", "2018-08-30")
vardiyalar = ("07:30 - 15:30", "15:30 - 23:30","23:30 - 07:30")

for i in gunler:
    for y in vardiyalar:
        for z in bolge:
           obj = VardiyalarRMS.objects.create(gun=i, bolge=z, vardiya_donemi=y)
    print(obj)
exit()

我的错误

    Traceback (most recent call last):
  File "C:/Users/17446/Desktop/aksamer/aksalist/management/commands/test_et.py", line 2, in <module>
    from aksalist.models import *
  File "C:\Users\17446\Desktop\aksamer\aksalist\models.py", line 6, in <module>
    class Birim(models.Model):
  File "C:\Users\17446\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\17446\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\17446\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Users\17446\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Users\17446\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

【问题讨论】:

标签: django django-management-command


【解决方案1】:

我认为您的settings.py 文件中的 INSTALLED_APPS 顺序不太正确。请尝试以下方法:

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

除非你直接调用test.py,在这种情况下,你需要在你的Django shell 环境中($ python manage.py shell)才能使用Models

【讨论】:

    【解决方案2】:

    您的代码需要在handle 方法中。因为它是在模块级别,所以在导入脚本时执行,在设置被正确激活之前。

    您必须在该句柄方法中添加pass 的事实应该为您提供了一些错误的线索。将其余代码放在那里。

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2020-09-27
      • 2020-04-12
      • 2018-10-09
      • 2019-12-02
      • 1970-01-01
      • 2020-03-07
      • 2019-04-22
      • 2012-09-13
      相关资源
      最近更新 更多