Django - 禁用系统检查之一

时间:2017-08-27 22:23:02

标签: python django django-manage.py

是否可以永久禁用 ONE (并非所有)系统检查(例如E301)?是否可以更改项目settings.py以跳过此系统检查所有./manage.py命令?

2 个答案:

答案 0 :(得分:6)

是的,您可以使用SILENCED_SYSTEM_CHECKS来阻止特定检查,例如

<强> settings.py

{IF bli}Text 2.1{ELSE blo}Text 2.1{ENDIF}

答案 1 :(得分:0)

要仅在某些条件下禁用一项检查(或多项检查),可以创建一个设置常量,在这种情况下,我将从环境变量中获取信息:

# 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