Django == 2.2.5
Python 3.7.3
yandex_general应用程序的结构:
.
├── admin.py
├── apps.py
├── __init__.py
├── models.py
├── validators.py
├── views.py
├── yandex_const.py
└── yandex_utils.py
validators.py
from yandex_general.yandex_utils import select_from_yandex_response
def validate_retargeting_condition_id(id: int):
id = select_from_yandex_response(AdEntity.RETARGETINGLISTS.value["service_name"], ids=[id])
if not id:
raise ValidationError("Отсутствует такой Идентификатор условия ретаргетинга")
models.py
from yandex_general.validators import validate_retargeting_condition_id
def validate_retargeting_condition_id(id: int):
id = select_from_yandex_response(AdEntity.RETARGETINGLISTS.value["service_name"], ids=[id])
if not id:
raise ValidationError("There is no such REtargeting condition")
yandex_utils.py
from yandex_general.models import YandexResponse
def select_from_yandex_response(yandex_direct_object_name: str, ids: int = None, name: str = None) -> dict:
try:
selection = getattr(YandexResponse.objects.get(key="yandex_response"), yandex_direct_object_name)
except ObjectDoesNotExist as e:
...
问题:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/home/michael/PycharmProjects/ads2/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/michael/PycharmProjects/ads2/ads_manager/yandex_general/models.py", line 3, in <module>
from yandex_general.validators import validate_retargeting_condition_id
File "/home/michael/PycharmProjects/ads2/ads_manager/yandex_general/validators.py", line 4, in <module>
from yandex_general.yandex_utils import select_from_yandex_response
File "/home/michael/PycharmProjects/ads2/ads_manager/yandex_general/yandex_utils.py", line 10, in <module>
from yandex_general.models import YandexResponse
ImportError: cannot import name 'YandexResponse' from 'yandex_general.models' (/home/michael/PycharmProjects/ads2/ads_manager/yandex_general/models.py)
如果将导入从模块顶部移至中间,则不会出现导入异常。
yandex_utils.py
def execute_request(ad_entity: AdEntity, data:dict, url: str = None) -> dict:
...
units = None
...
if units:
from yandex_general.models import YandexResponse
yandex_general, _ = YandexResponse.objects.get_or_create(key="yandex_response")
但是在这种情况下,无论何时需要在yandex_utils.py中,我都必须多次导入YandexResponse。自动导入排序(isort)将破坏我的代码。
我的想法:
yandex_utils.py导入 yandex_general.models导入YandexResponse)。
yandex_general.models不是从yandex_utils.py导入,而是导入 来自验证者(来自yandex_general.validators import validate_retargeting_condition_id)。
validators.py(来自 yandex_general.yandex_utils导入select_from_yandex_response)。
也许这是循环进口? 我该如何应对?