如何将模型导入到与models.py相同的文件夹中的文件?

时间:2018-08-13 19:19:12

标签: python django

我想将模型导入到dashboards.py所在的文件夹中的models.py文件中:

#dashboards.py
from models import Listing, City 

运行文件会导致:

Traceback (most recent call last):
  File "dashboards.py", line 6, in <module>
    from models import Listing, City #, ScrapingDate
  File "/home/bart/python_projects/javascript/models.py", line 19, in <module>
    class City(models.Model):
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

如果我更改为:

#dashboards.py
from .models import Listing, City 

我得到:

Traceback (most recent call last):
  File "dashboards.py", line 6, in <module>
    from .models import Listing, City #, ScrapingDate
SystemError: Parent module '' not loaded, cannot perform relative import

我也尝试过:

#dashboards.py
import django
django.setup()
from models import Listing, City

但是:

Traceback (most recent call last):
  File "dashboards.py", line 2, in <module>
    django.setup()
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

有什么解决方法吗?

0 个答案:

没有答案