系统检查框架

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

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

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

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

    The check function must accept an appconfigs argument; this argument isthe list of applications that should be inspected. If None, the check must berun on _all installed apps in the project. The **kwargs argument is requiredfor future expansion.

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

    The concept is very similar to messages from the 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 isimplied by the class name.

    Lastly, your check function must be registered explicitly with system checkregistry. Checks should be registered in a file that's loaded when yourapplication is loaded; for example, in the method.

    • (*tags)(function)
    • You can pass as many tags to register as you want in order to label yourcheck. Tagging checks is useful since it allows you to run only a certaingroup of checks. For example, to register a compatibility check, you wouldmake the following call:
    1. from django.core.checks import register, Tags
    2.  
    3. @register(Tags.compatibility)
    4. def my_check(app_configs, **kwargs):
    5. # ... perform compatibility checks and collect errors

    You can register "deployment checks" that are only relevant to a productionsettings file like this:

    You can also use register as a function rather than a decorator bypassing a callable object (usually a function) as the first argumentto register.

    The code below is equivalent to the code above:

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

    In some cases, you won't need to register your check function — you canpiggyback on an existing registration.

    Fields, models, model managers, and database backends all implement acheck() method that is already registered with the check framework. If youwant 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 generatedby the base class. It's recommended that you delegate each check to separatemethods.

    Consider an example where you are implementing a custom field namedRangedIntegerField. This field adds min and max arguments to theconstructor of . You may want to add a check to ensure that usersprovide a min value that is less than or equal to the max value. The followingcode snippet shows how you can implement this check:

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

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

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