我有“请求设置INSTALLED_APPS”警告

时间:2018-08-27 12:25:59

标签: django django-management-command

我正在尝试从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.

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的代码必须位于handle方法内。实际上,它是在模块级别,因此在导入脚本时会在正确激活设置之前执行该操作。

您必须在该handle方法中放置一个pass的事实应该为您提供了一个提示错误的线索。将其余代码放在那里。