System check framework

The system check framework is a set of static checks for validating Django projects. It detects common problems and provides hints for how to fix them. The framework is extensible so you can easily add your own checks.

Checks can be triggered explicitly via the check command. Checks are triggered implicitly before most commands, including runserver and migrate. For performance reasons, checks are not run as part of the WSGI stack that is used in deployment. If you need to run system checks on your deployment server, trigger them explicitly using check.

Serious errors will prevent Django commands (such as runserver) from running at all. Minor problems are reported to the console. If you have inspected the cause of a warning and are happy to ignore it, you can hide specific warnings using the SILENCED_SYSTEM_CHECKS setting in your project settings file.

A full list of all checks that can be raised by Django can be found in the System check reference.

Writing your own checks

The framework is flexible and allows you to write functions that perform any other kind of check you may require. The following is an example stub check function:

  1. from django.core.checks import Error, register
  2. @register()
  3. def example_check(app_configs, **kwargs):
  4. errors = []
  5. # ... your check logic here
  6. if check_failed:
  7. errors.append(
  8. Error(
  9. "an error",
  10. hint="A hint.",
  11. obj=checked_object,
  12. id="myapp.E001",
  13. )
  14. )
  15. return errors

The check function must accept an app_configs argument; this argument is the list of applications that should be inspected. If None, the check must be run on all installed apps in the project.

The check will receive a databases keyword argument. This is a list of database aliases whose connections may be used to inspect database level configuration. If databases is None, the check must not use any database connections.

The **kwargs argument is required for future expansion.

Messages

The function must return a list of messages. If no problems are found as a result of the check, the check function must return an empty list.

The warnings and errors raised by the check method must be instances of CheckMessage. An instance of CheckMessage encapsulates a single reportable error or warning. It also provides context and hints applicable to the message, and a unique identifier that is used for filtering purposes.

The concept is very similar to messages from the message framework or the logging framework. Messages are tagged with a level indicating the severity of the message.

There are also shortcuts to make creating messages with common levels easier. When using these classes you can omit the level argument because it is implied by the class name.

Registering and labeling checks

Lastly, your check function must be registered explicitly with system check registry. Checks should be registered in a file that’s loaded when your application is loaded; for example, in the AppConfig.ready() method.

register(*tags)(function)

You can pass as many tags to register as you want in order to label your check. Tagging checks is useful since it allows you to run only a certain group of checks. For example, to register a compatibility check, you would make the following call:

  1. from django.core.checks import register, Tags
  2. @register(Tags.compatibility)
  3. def my_check(app_configs, **kwargs):
  4. # ... perform compatibility checks and collect errors
  5. return errors

You can register “deployment checks” that are only relevant to a production settings file like this:

  1. @register(Tags.security, deploy=True)
  2. def my_check(app_configs, **kwargs): ...

These checks will only be run if the check —deploy option is used.

You can also use register as a function rather than a decorator by passing a callable object (usually a function) as the first argument to register.

The code below is equivalent to the code above:

  1. def my_check(app_configs, **kwargs): ...
  2. register(my_check, Tags.security, deploy=True)

Field, model, manager, template engine, and database checks

In some cases, you won’t need to register your check function – you can piggyback on an existing registration.

Fields, models, model managers, template engines, and database backends all implement a check() method that is already registered with the check framework. If you want to add extra checks, you can extend the implementation on the base class, perform any extra checks you need, and append any messages to those generated by the base class. It’s recommended that you delegate each check to separate methods.

Consider an example where you are implementing a custom field named RangedIntegerField. This field adds min and max arguments to the constructor of IntegerField. You may want to add a check to ensure that users provide a min value that is less than or equal to the max value. The following code snippet shows how you can implement this check:

  1. from django.core import checks
  2. from django.db import models
  3. class RangedIntegerField(models.IntegerField):
  4. def __init__(self, min=None, max=None, **kwargs):
  5. super().__init__(**kwargs)
  6. self.min = min
  7. self.max = max
  8. def check(self, **kwargs):
  9. # Call the superclass
  10. errors = super().check(**kwargs)
  11. # Do some custom checks and add messages to `errors`:
  12. errors.extend(self._check_min_max_values(**kwargs))
  13. # Return all errors and warnings
  14. return errors
  15. def _check_min_max_values(self, **kwargs):
  16. if self.min is not None and self.max is not None and self.min > self.max:
  17. return [
  18. checks.Error(
  19. "min greater than max.",
  20. hint="Decrease min or increase max.",
  21. obj=self,
  22. id="myapp.E001",
  23. )
  24. ]
  25. # When no error, return an empty list
  26. return []

If you wanted to add checks to a model manager, you would take the same approach on your subclass of Manager.

If you want to add a check to a model class, the approach is almost the same: the only difference is that the check is a classmethod, not an instance method:

  1. class MyModel(models.Model):
  2. @classmethod
  3. def check(cls, **kwargs):
  4. errors = super().check(**kwargs)
  5. # ... your own checks ...
  6. return errors

Changed in Django 5.1:

In older versions, template engines didn’t implement a check() method.

Writing tests

Messages are comparable. That allows you to easily write tests:

  1. from django.core.checks import Error
  2. errors = checked_object.check()
  3. expected_errors = [
  4. Error(
  5. "an error",
  6. hint="A hint.",
  7. obj=checked_object,
  8. id="myapp.E001",
  9. )
  10. ]
  11. self.assertEqual(errors, expected_errors)

Writing integration tests

Given the need to register certain checks when the application loads, it can be useful to test their integration within the system checks framework. This can be accomplished by using the call_command() function.

For example, this test demonstrates that the SITE_ID setting must be an integer, a built-in check from the sites framework:

  1. from django.core.management import call_command
  2. from django.core.management.base import SystemCheckError
  3. from django.test import SimpleTestCase, modify_settings, override_settings
  4. class SystemCheckIntegrationTest(SimpleTestCase):
  5. @override_settings(SITE_ID="non_integer")
  6. @modify_settings(INSTALLED_APPS={"prepend": "django.contrib.sites"})
  7. def test_non_integer_site_id(self):
  8. message = "(sites.E101) The SITE_ID setting must be an integer."
  9. with self.assertRaisesMessage(SystemCheckError, message):
  10. call_command("check")

Consider the following check which issues a warning on deployment if a custom setting named ENABLE_ANALYTICS is not set to True:

  1. from django.conf import settings
  2. from django.core.checks import Warning, register
  3. @register("myapp", deploy=True)
  4. def check_enable_analytics_is_true_on_deploy(app_configs, **kwargs):
  5. errors = []
  6. if getattr(settings, "ENABLE_ANALYTICS", None) is not True:
  7. errors.append(
  8. Warning(
  9. "The ENABLE_ANALYTICS setting should be set to True in deployment.",
  10. id="myapp.W001",
  11. )
  12. )
  13. return errors

Given that this check will not raise a SystemCheckError, the presence of the warning message in the stderr output can be asserted like so:

  1. from io import StringIO
  2. from django.core.management import call_command
  3. from django.test import SimpleTestCase, override_settings
  4. class EnableAnalyticsDeploymentCheckTest(SimpleTestCase):
  5. @override_settings(ENABLE_ANALYTICS=None)
  6. def test_when_set_to_none(self):
  7. stderr = StringIO()
  8. call_command("check", "-t", "myapp", "--deploy", stderr=stderr)
  9. message = (
  10. "(myapp.W001) The ENABLE_ANALYTICS setting should be set "
  11. "to True in deployment."
  12. )
  13. self.assertIn(message, stderr.getvalue())