Django “ImproperlyConfigured”, but not always

守給你的承諾、 提交于 2021-02-19 08:01:22

问题


I'm starting a new Django project, using Pycharm 4. I have defined the various models, and if I run tests, such as this,

from django.test import TestCase
from models import OwnerDatabase

class TestZero(TestCase):
    def test_Settings(self):
        item=OwnerDatabase.objects.first

everything works. But in a different part of the project, if I have this code (a standalone script)

from models import OwnerDatabase

the program balks with the following traceback

  File "/home/piffy/PycharmProjects/Russeau/russeau/datasetcreate.py", line 9, in <module>
    from russeau.models import OwnerDatabase, PublishedDatabase
  File "/home/piffy/PycharmProjects/Russeau/russeau/models.py", line 4, in <module>
    class OwnerDatabase(models.Model):
  File "/home/piffy/PycharmProjects/Russeau/russeau/models.py", line 5, in OwnerDatabase
    time = models.DateTimeField()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1098, in __init__
    super(DateField, self).__init__(verbose_name, name, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 146, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 40, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

But, in manage.py I already have

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Russeau.settings")

Though I'm relatively new to Django and Python, my other projects had not such problems. I'm also at a loss as to how to go on.

Thanks!

--- Updated:

Thanks to Daniel, below, I added

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Russeau.settings")

So the error now changed to

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

On the good track, but..?


回答1:


If it's a standalone script, you need to setup Django settings first. In this case, manage.py has nothing to do with it because you are not using it to call your script. To fix this, you have two options:

  1. Write this at the top of your script:

    sys.path.append("/path/to/project") #Set it to the root of your project
    os.environ["DJANGO_SETTINGS_MODULE"] = "<project>.settings"
    django.setup()
    

    After that, you should be able to import your models or whatever you need from your project. You can check this for more detailed explanation if you want.

  2. Create a custom management command so you will be able to call python manage.py mycommand.



来源:https://stackoverflow.com/questions/29828019/django-improperlyconfigured-but-not-always

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!