Using Django's system check framework
David Seddon
david@seddonym.me
http://seddonym.me
$ ./manage.py runserver
...
django.core.management.base.SystemCheckError: SystemCheckError:
System check identified some issues:
ERRORS:
main.MyModel.my_charfield: (fields.E120) CharFields must define a 'max_length' attribute.
System check identified 1 issue (0 silenced).
System checks are automatic
$ ./manage.py check
...
django.core.management.base.SystemCheckError: SystemCheckError:
System check identified some issues:
ERRORS:
main.MyModel.my_charfield: (fields.E120) CharFields must define a 'max_length' attribute.
System check identified 1 issue (0 silenced).
$
System checks can be run manually
For performance reasons, checks are not run as part of the WSGI stack that is used in deployment.
System checks are extensible
Define your own custom system checks on an app-by-app basis
System checks are easy
That's why this is a short talk
Why use it?
Example
Let's say you never want to have DEBUG as True in production.
IS_PRODUCTION = True # Custom setting for the production site
DEBUG = True # This should never happen!
Your system check could make sure these two settings are never both True.
Operating instructions
# rock_n_roll/__init__.py
from django.core.checks import Error, register
from django.conf import settings
@register()
def my_check(app_configs, **kwargs):
errors = []
if settings.IS_PRODUCTION and settings.DEBUG:
errors.append(
Error(
'IS_PRODUCTION and DEBUG are both True.',
hint='DEBUG should never be True on production, check your settings file.',
id='rock_n_roll.E001',
)
)
return errors
$ ./manage.py check
ERRORS:
?: (rock_n_roll.E001) IS_PRODUCTION and DEBUG are both True.
HINT: DEBUG should never be True on production,
check your settings file.
System check identified 1 issue (0 silenced).
https://slides.com/davidseddon/check-the-system
David Seddon
david@seddonym.me
http://seddonym.me