Django - 禁用系统检查之一
Django - disable one of system checks
是否可以永久禁用 ONE(不是全部)系统检查(例如 E301)?是否可以更改项目 settings.py 以跳过所有 ./manage.py 命令的系统检查?
是的,您可以使用 SILENCED_SYSTEM_CHECKS 来禁止特定检查,例如
settings.py
SILENCED_SYSTEM_CHECKS = ["models.W001"]
要仅在某些条件下禁用检查(或多次检查),您可以创建一个设置常量,在这种情况下,我从环境变量中获取信息:
# 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'
希望对您有所帮助!
更新:某些 manage.py 命令将 运行 检查而不考虑 SILENCED_SYSTEM_CHECKS
值。为此,我有一个特殊的解决方法:
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
是否可以永久禁用 ONE(不是全部)系统检查(例如 E301)?是否可以更改项目 settings.py 以跳过所有 ./manage.py 命令的系统检查?
是的,您可以使用 SILENCED_SYSTEM_CHECKS 来禁止特定检查,例如
settings.py
SILENCED_SYSTEM_CHECKS = ["models.W001"]
要仅在某些条件下禁用检查(或多次检查),您可以创建一个设置常量,在这种情况下,我从环境变量中获取信息:
# 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'
希望对您有所帮助!
更新:某些 manage.py 命令将 运行 检查而不考虑 SILENCED_SYSTEM_CHECKS
值。为此,我有一个特殊的解决方法:
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