【发布时间】:2017-08-27 22:23:01
【问题描述】:
是否可以只永久禁用一个(不是全部)系统检查(例如 E301)?是否可以更改项目 settings.py 以跳过对所有 ./manage.py 命令的系统检查?
【问题讨论】:
标签: python django django-manage.py
是否可以只永久禁用一个(不是全部)系统检查(例如 E301)?是否可以更改项目 settings.py 以跳过对所有 ./manage.py 命令的系统检查?
【问题讨论】:
标签: python django django-manage.py
【讨论】:
要仅在某些条件下禁用检查(或多次检查),您可以创建一个设置常量,在这种情况下,我从环境变量中获取信息:
# Disable checks, i.e. for build process
DISABLE_CHECKS = os.getenv('DISABLE_CHECKS') in ('1', 1, True)
if DISABLE_CHECKS:
SILENCED_SYSTEM_CHECKS = ['content_services.E001', 'content_services.E002']
检查的名称是您在错误消息中指定的id。这是一个示例检查:
def check_cache_connectivity(app_configs, **kwargs):
"""
Check cache
:param app_configs:
:param kwargs:
:return:
"""
errors = []
cache_settings = settings.CACHES.keys()
for cur_cache in cache_settings:
try:
key = 'check_cache_connectivity_{}'.format(cur_cache)
caches[cur_cache].set(key, 'connectivity_ok', 30)
value = caches[cur_cache].get(key)
print("Cache '{}' connection ok, key '{}', value '{}'".format(cur_cache, key, value))
except Exception as e:
msg = "ERROR: Cache {} looks to be down. {}".format(cur_cache, e)
print(msg)
logging.exception(msg)
errors.append(
Error(
msg,
hint='Unable to connect to cache {}. {}'.format(cur_cache, e),
obj='CACHES.{}'.format(cur_cache),
id='content_services.E002',
)
)
return errors
我启动这些检查的方式是在我的apps.py 中针对特定应用程序,即:
from django.apps import AppConfig
from django.core import checks
from common_checks import check_db_connectivity
from common_checks import check_cache_connectivity
class ContentAppConfig(AppConfig):
name = 'content_app'
def ready(self):
super(ContentAppConfig, self).ready()
checks.register(checks.Tags.compatibility)(check_cache_connectivity)
在我的应用程序__init__.py 中,我还设置了默认应用程序配置:
default_app_config = 'content_app.apps.ContentAppConfig'
希望对你有帮助!
更新:无论SILENCED_SYSTEM_CHECKS 值如何,一些manage.py 命令都会运行检查。为此,我有一个特殊的解决方法:
def check_cache_connectivity(app_configs, **kwargs):
"""
Check cache
:param app_configs:
:param kwargs:
:return:
"""
errors = []
# Short circuit here, checks still ran by manage.py cmds regardless of SILENCED_SYSTEM_CHECKS
if settings.DISABLE_CHECKS:
return errors
cache_settings = settings.CACHES.keys()
for cur_cache in cache_settings:
try:
key = 'check_cache_connectivity_{}'.format(cur_cache)
caches[cur_cache].set(key, 'connectivity_ok', 30)
value = caches[cur_cache].get(key)
print("Cache '{}' connection ok, key '{}', value '{}'".format(cur_cache, key, value))
except Exception as e:
msg = "ERROR: Cache {} looks to be down. {}".format(cur_cache, e)
print(msg)
logging.exception(msg)
errors.append(
Error(
msg,
hint="Unable to connect to cache {}, set as {}. {}"
"".format(cur_cache, settings.CACHES[cur_cache], e),
obj='CACHES.{}'.format(cur_cache),
id='content_services.E002',
)
)
return errors
【讨论】: