Check the System

Using Django's system check framework 

 

 

David Seddon

david@seddonym.me

http://seddonym.me

Who has used the system check framework?

Who has written custom checks?

Django's System Check framework

  1. What it does
  2. Why you might use it
  3. Operating instructions

What it does

Validates your Django projects

$ ./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

Checks are triggered implicitly before most commands, including runserverand migrate.

$ ./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?

 

  1. Validating configuration
  2. Validating mini frameworks / APIs
  3. Any issues that you want to know about at 'compile time'

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

  1. Write a system check function.
  2. Register it.
  3. Make sure the module is loaded.

Write a check function

# 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

The results

$ ./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).

Any other use cases?

 

https://slides.com/davidseddon/check-the-system

 

 

 

David Seddon

david@seddonym.me

http://seddonym.me

 

Made with Slides.com