[Solved]-Django – disable one of system checks

20👍

Yes you can use SILENCED_SYSTEM_CHECKS to silence specific checks e.g.

settings.py

SILENCED_SYSTEM_CHECKS = ["models.W001"]

2👍

To disable a check (or many checks) under certain conditions only, you can create a settings constant, and in this case I am getting the information from an environment variable:

# 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']

The name of the check is the id you assign it in your error message. Here is a sample check:

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

The way I initiate these checks are in my apps.py for the particular app i.e.:

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)

In my apps __init__.py I also set the default app config:

default_app_config = 'content_app.apps.ContentAppConfig'

Hope it helps!

UPDATE: Some manage.py commands will run checks regardless of the SILENCED_SYSTEM_CHECKS value. For this I have a special workaround:

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
👤radtek

Leave a comment