0

我正在尝试从BaseCommandDjango 中的类扩展。但是,在运行时$ 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.
4

2 回答 2

1

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

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

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

于 2018-08-27T12:36:46.457 回答
0

您的代码需要在handle方法内。事实上,它处于模块级别,因此在导入脚本时执行,在设置被正确激活之前。

您必须在该句柄方法中放入一个事实pass应该给您一个线索,表明某些东西是错误的。将其余代码放在那里。

于 2018-08-27T14:38:18.497 回答