============= 系统检查框架 ============= .. currentmodule:: django.core.checks 系统检查框架是一组用于验证Django项目的静态检查。 它检测常见问题并提供如何修复它们的提示。框架是可扩展的,因此您可以轻松添加自定义的检查。 有关如何添加自定义检查并将其与Django的系统检查集成的详细信息,请参阅 :doc:`System check topic guide `。 API参考 ========= ``CheckMessage`` ----------------- .. class:: CheckMessage(level, msg, hint=None, obj=None, id=None) 系统检查引起的警告和错误必须是 ``CheckMessage`` 的实例。 这个实例封装了一个单一的可重复使用的错误和警告。 它还提供了用于消息的上下文和提示,以及用于过滤的惟一标识符。 构造函数的参数: ``level`` 信息的严重性。使用一个预定义值: ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``, ``CRITICAL``. 如果级别大于或等于 ``ERROR``, 则Django将阻止管理命令执行。如果消息等级下雨 ``ERROR`` (i.e. warnings) 将报告给控制台, 但不做其他处理。 ``msg`` 对问题简短描述的字符串 (少于 80 个字符),字符串不能包含换行符。 ``hint`` 为解决问题提供提示的单行字符串。如果没有提供任何提示, 或者提示从错误消息中可以明显看出,则提示可以省略,或者用``None``。 ``obj`` 可选。为消息提供上下文的对象(例如,发现问题的模型)。对象应该是一个模型、字段、管理器或定义 了 ``__str__`` 方法的任何其他对象(在Python 2中,您需要定义 ``__unicode__`` 方法)。 在报告所有消息时使用该方法。 ``id`` 可选字符串,问题的唯一标示。标识符应该遵循这种模式 ``applabel.X001`` 。``X`` 是一个 ``CEWID`` 的一个字母,表示消息严重程度( ``C`` 代表 ``CRITICAL``, ``E`` 代表 ``ERROR`` 等。) 这个数字可以由应用程序任意分配,但在应用程序中应该是唯一的。 有子类可以使创建具有公共级别的消息更加容易。在使用它们时,您可以省略 ``level`` 参数,因为它是由类名隐含的 .. class:: Debug(msg, hint=None, obj=None, id=None) .. class:: Info(msg, hint=None, obj=None, id=None) .. class:: Warning(msg, hint=None obj=None, id=None) .. class:: Error(msg, hint=None, obj=None, id=None) .. class:: Critical(msg, hint=None, obj=None, id=None) 内建检查 ========= .. _system-check-builtin-tags: 内建tags ------------ Django的系统检查使用以下标记: * ``models``: 检查管理模型,字段和管理器定义。 * ``signals``: 检查信号声明和处理程序注册。 * ``admin``: 检查所有管理网站声明。 * ``compatibility``: 标记版本升级的潜在问题。 * ``security``: 检查安全相关的配置。 * ``templates``: 检查模板相关配置。 * ``caches``: 检查缓存相关配置。 * ``urls``: 检查路由相关配置。 * ``database``: 检查数据库配置相关问题。数据库检查并不是以默认方式运行,因为它们比静态的代码分析做的更多。 它们只由 :djadmin:`migrate` 命令运行,或者在调用 :djadmin:`check` 命令时指定数据库标记。 .. versionadded:: 1.10 ``database`` tag 在再1.10版本开始加入的。 某些检查可能会向多个标签注册。 核心系统检查 ------------ Models ~~~~~~ * **models.E001**: ```` 格式不是 ``app_label.app_name``. * **models.E002**: ```` 引用的 ```` 没有被 installed,或者是抽象的。 * **models.E003**: 该模型通过中介模型 ``.`` 有两个多对多关系。 * **models.E004**: ``id`` 只能用于设置了 ``primary_key=True`` 的字段名称。 * **models.E005**: ```` 中的字段 ```` 有冲突。 * **models.E006**: 该字段和模型 ```` 中的字段 ```` 存在冲突。 * **models.E007**: 字段 ```` 的列名 ```` 已经被其他字段使用了。 * **models.E008**: ``index_together`` 必须是列表或者元组。 * **models.E009**: 所有 ``index_together`` 必须是列表或者元组。 * **models.E010**: ``unique_together`` 必须是列表或者元组。 * **models.E011**: 所有 ``unique_together`` 必须是列表或者元组。 * **models.E012**: ``index_together/unique_together`` 关联到了不存在的字段名 ````。 * **models.E013**: ``index_together/unique_together`` 关联了 ``ManyToManyField`` ````, 但是 ``ManyToManyField`` 不支持该选项。 * **models.E014**: ``ordering`` 必须是列表或者元组 ( 即使你只想按一个字段排序)。 * **models.E015**: ``ordering`` 关联到了一个存在的 ````。 * **models.E016**: ``index_together/unique_together`` 关联的字段 ```` 不在本地模型 ```` 中。 * **models.E017**: 代理模型 ```` 不能有模型字段。 * **models.E018**: 字段 ```` 的自动生成列名过长。数据库 ```` 中的最大长度是 ````。 * **models.E019**: M2M字段 ```` 的自动生成列名过长。 数据库 ```` 中的最大长度是 ````。 * **models.E020**: ``.check()`` 类方法当前被覆盖。 * **models.E021**: ``ordering`` 和 ``order_with_respect_to`` 不能同时使用。 * **models.E022**: ```` 包含了 ``.`` 的惰性引用, 但是应用 ```` 没有install或者没有模型 ```` 。 字段 ~~~~~~ * **fields.E001**: 字段名称不能以下划线结尾。 * **fields.E002**: 字段名称不能包含``"__"`` 。 * **fields.E003**: ``pk`` 是不能用作字段名称的保留字。 * **fields.E004**: ``choices`` 必须是可迭代的 (e.g., 元组或者列表). * **fields.E005**: ``choices`` 必须是可迭代的返回 ``(实际值,易读值)`` 元组。 * **fields.E006**: ``db_index`` 必须是 ``None``, ``True`` 或者 ``False``。 * **fields.E007**: 主键必须设置 ``null=True``。 * **fields.E100**: ``AutoField`` 必须设置 primary_key=True. * **fields.E110**: ``BooleanField`` 不接受null。 * **fields.E120**: ``CharField`` 必须定义 ``max_length`` 属性。 * **fields.E121**: ``max_length`` 必须是正整数。 * **fields.W122**: 使用 ``IntegerField`` 时可忽略 ``max_length`` 。 * **fields.E130**: ``DecimalField`` 必须定义 ``decimal_places`` 属性。 * **fields.E131**: ``decimal_places`` 必须是非负整数。 * **fields.E132**: ``DecimalField` 必须定义 ``max_digits`` 属性。 * **fields.E133**: ``max_digits`` 必须是非负整数。 * **fields.E134**: ``max_digits`` 必须大于等于 ``decimal_places``。 * **fields.E140**: ``FilePathField`` 必须设置 ``allow_files`` 或者 ``allow_folders`` 为True。 * **fields.E150**: ``GenericIPAddressField`` 如果不允许空值, 则不能接受空值,,因为空值存储为null。 * **fields.E160**: 选项 ``auto_now``, ``auto_now_add`` 和 ``default`` 是互斥的。这些选项中只能有一个存在。 * **fields.W161**: 提供固定的默认值。 * **fields.E900**: ``IPAddressField`` 已被删除,仅在历史迁移中支持。 * **fields.W900**: ``IPAddressField`` 已被弃用。对它的支持(除了历史迁移)将在Django 1.9中删除。 * 这个检查只在 Django 1.7 和 1.8 中* 。 * **fields.W901**: ``CommaSeparatedIntegerField`` 已弃用。对它的支持(除了在历史迁移)将在Django 2.0中删除。 文件字段 ~~~~~~~~~ * **fields.E200**: ``unique`` 不是 ``FileField`` 的合法参数。 * **fields.E201**: ``primary_key`` 不是 ``FileField`` 的合法参数。 * **fields.E210**: 由于 Pillow 没有安装,所以 ``ImageField`` 无法使用。 关系字段 ~~~~~~~~~ * **fields.E300**: 字段定义的关系模型 ```` 没有install或者是抽象的。 * **fields.E301**: 字段定义的关系模型 ```` 已经换出。 * **fields.E302**: 访问字段 ```` 与字段 ```` 冲突。 * **fields.E303**: 反向查询字段 ```` 与字段 ```` 冲突。 * **fields.E304**: 字段 ```` 与访问字段 ```` 冲突。 * **fields.E305**: 字段 ```` 与反向查询字段 ```` 冲突。 * **fields.E306**: 关系名必须是Python标识符,或者以 ``'+'`` 结尾。 * **fields.E307**: 字段 ``..`` 是惰性关联到模型 ``.``, 但是应用 ```` 并没有install,或是没有此模型 ````。 * **fields.E310**: No subset of the fields ````, ````, ... on model ```` is unique. Add ``unique=True`` on any of those fields or add at least a subset of them to a unique_together constraint. * **fields.E311**: ```` must set ``unique=True`` because it is referenced by a ``ForeignKey``. * **fields.E320**: Field specifies ``on_delete=SET_NULL``, but cannot be null. * **fields.E321**: The field specifies ``on_delete=SET_DEFAULT``, but has no default value. * **fields.E330**: ``ManyToManyField``\s cannot be unique. * **fields.E331**: Field specifies a many-to-many relation through model ````, which has not been installed. * **fields.E332**: Many-to-many fields with intermediate tables must not be symmetrical. * **fields.E333**: The model is used as an intermediate model by ````, but it has more than two foreign keys to ````, which is ambiguous. You must specify which two foreign keys Django should use via the ``through_fields`` keyword argument. * **fields.E334**: The model is used as an intermediate model by ````, but it has more than one foreign key from ````, which is ambiguous. You must specify which foreign key Django should use via the ``through_fields`` keyword argument. * **fields.E335**: The model is used as an intermediate model by ````, but it has more than one foreign key to ````, which is ambiguous. You must specify which foreign key Django should use via the ``through_fields`` keyword argument. * **fields.E336**: The model is used as an intermediary model by ````, but it does not have foreign key to ```` or ````. * **fields.E337**: Field specifies ``through_fields`` but does not provide the names of the two link fields that should be used for the relation through ````. * **fields.E338**: The intermediary model ```` has no field ````. * **fields.E339**: ``.`` is not a foreign key to ````. * **fields.W340**: ``null`` has no effect on ``ManyToManyField``. * **fields.W341**: ``ManyToManyField`` does not support ``validators``. * **fields.W342**: Setting ``unique=True`` on a ``ForeignKey`` has the same effect as using a ``OneToOneField``. Signals ~~~~~~~ * **signals.E001**: ```` was connected to the ```` signal with a lazy reference to the sender ``.``, but app ```` isn't installed or doesn't provide model ````. Backwards Compatibility ~~~~~~~~~~~~~~~~~~~~~~~ The following checks are performed to warn the user of any potential problems that might occur as a result of a version upgrade. * **1_6.W001**: Some project unit tests may not execute as expected. *This check was removed in Django 1.8 due to false positives*. * **1_6.W002**: ``BooleanField`` does not have a default value. *This check was removed in Django 1.8 due to false positives*. * **1_7.W001**: Django 1.7 changed the global defaults for the ``MIDDLEWARE_CLASSES.`` ``django.contrib.sessions.middleware.SessionMiddleware``, ``django.contrib.auth.middleware.AuthenticationMiddleware``, and ``django.contrib.messages.middleware.MessageMiddleware`` were removed from the defaults. If your project needs these middleware then you should configure this setting. *This check was removed in Django 1.9*. * **1_8.W001**: The standalone ``TEMPLATE_*`` settings were deprecated in Django 1.8 and the :setting:`TEMPLATES` dictionary takes precedence. You must put the values of the following settings into your defaults ``TEMPLATES`` dict: ``TEMPLATE_DIRS``, ``TEMPLATE_CONTEXT_PROCESSORS``, ``TEMPLATE_DEBUG``, ``TEMPLATE_LOADERS``, ``TEMPLATE_STRING_IF_INVALID``. * **1_10.W001**: The ``MIDDLEWARE_CLASSES`` setting is deprecated in Django 1.10 and the :setting:`MIDDLEWARE` setting takes precedence. Since you've set ``MIDDLEWARE``, the value of ``MIDDLEWARE_CLASSES`` is ignored. Admin ----- Admin checks are all performed as part of the ``admin`` tag. The following checks are performed on any :class:`~django.contrib.admin.ModelAdmin` (or subclass) that is registered with the admin site: * **admin.E001**: The value of ``raw_id_fields`` must be a list or tuple. * **admin.E002**: The value of ``raw_id_fields[n]`` refers to ````, which is not an attribute of ````. * **admin.E003**: The value of ``raw_id_fields[n]`` must be a foreign key or a many-to-many field. * **admin.E004**: The value of ``fields`` must be a list or tuple. * **admin.E005**: Both ``fieldsets`` and ``fields`` are specified. * **admin.E006**: The value of ``fields`` contains duplicate field(s). * **admin.E007**: The value of ``fieldsets`` must be a list or tuple. * **admin.E008**: The value of ``fieldsets[n]`` must be a list or tuple. * **admin.E009**: The value of ``fieldsets[n]`` must be of length 2. * **admin.E010**: The value of ``fieldsets[n][1]`` must be a dictionary. * **admin.E011**: The value of ``fieldsets[n][1]`` must contain the key ``fields``. * **admin.E012**: There are duplicate field(s) in ``fieldsets[n][1]``. * **admin.E013**: ``fields[n]/fieldsets[n][m]`` cannot include the ``ManyToManyField`` ````, because that field manually specifies a relationship model. * **admin.E014**: The value of ``exclude`` must be a list or tuple. * **admin.E015**: The value of ``exclude`` contains duplicate field(s). * **admin.E016**: The value of ``form`` must inherit from ``BaseModelForm``. * **admin.E017**: The value of ``filter_vertical`` must be a list or tuple. * **admin.E018**: The value of ``filter_horizontal`` must be a list or tuple. * **admin.E019**: The value of ``filter_vertical[n]/filter_vertical[n]`` refers to ````, which is not an attribute of ````. * **admin.E020**: The value of ``filter_vertical[n]/filter_vertical[n]`` must be a many-to-many field. * **admin.E021**: The value of ``radio_fields`` must be a dictionary. * **admin.E022**: The value of ``radio_fields`` refers to ````, which is not an attribute of ````. * **admin.E023**: The value of ``radio_fields`` refers to ````, which is not a ``ForeignKey``, and does not have a ``choices`` definition. * **admin.E024**: The value of ``radio_fields[]`` must be either ``admin.HORIZONTAL`` or ``admin.VERTICAL``. * **admin.E025**: The value of ``view_on_site`` must be either a callable or a boolean value. * **admin.E026**: The value of ``prepopulated_fields`` must be a dictionary. * **admin.E027**: The value of ``prepopulated_fields`` refers to ````, which is not an attribute of ````. * **admin.E028**: The value of ``prepopulated_fields`` refers to ````, which must not be a ``DateTimeField``, a ``ForeignKey``, or a ``ManyToManyField`` field. * **admin.E029**: The value of ``prepopulated_fields[]`` must be a list or tuple. * **admin.E030**: The value of ``prepopulated_fields`` refers to ````, which is not an attribute of ````. * **admin.E031**: The value of ``ordering`` must be a list or tuple. * **admin.E032**: The value of ``ordering`` has the random ordering marker ``?``, but contains other fields as well. * **admin.E033**: The value of ``ordering`` refers to ````, which is not an attribute of ````. * **admin.E034**: The value of ``readonly_fields`` must be a list or tuple. * **admin.E035**: The value of ``readonly_fields[n]`` is not a callable, an attribute of ````, or an attribute of ````. ``ModelAdmin`` ~~~~~~~~~~~~~~ The following checks are performed on any :class:`~django.contrib.admin.ModelAdmin` that is registered with the admin site: * **admin.E101**: The value of ``save_as`` must be a boolean. * **admin.E102**: The value of ``save_on_top`` must be a boolean. * **admin.E103**: The value of ``inlines`` must be a list or tuple. * **admin.E104**: ```` must inherit from ``BaseModelAdmin``. * **admin.E105**: ```` must have a ``model`` attribute. * **admin.E106**: The value of ``.model`` must be a ``Model``. * **admin.E107**: The value of ``list_display`` must be a list or tuple. * **admin.E108**: The value of ``list_display[n]`` refers to ``