每次 runserver 或 migrate 前面那句 “System check identified no issues” 是什么?你能往里加自己的检查吗?Error 和 Warning 差在哪?这篇写三个自定义检查,在真实项目里把它们的作用范围逐个验证。
rc=0 System check identified no issues (0 silenced).
check → rc=1
SystemCheckError: System check identified some issues:
ERRORS:
?: (lab.E001) 自定义检查:发现一个错误
HINT: 把它修好
System check identified 1 issue (0 silenced).
migrate → rc=1 最后一行: HINT: 把它修好
runserver → rc=1 最后一行: System check identified 1 issue (0 silenced).用 @register(Tags.models) 装饰一个返回 Error 列表的函数即可加入检查;检查在 AppConfig.ready() 里导入(这样才会注册)。
check → rc=0
System check identified some issues:
WARNINGS:
?: (lab.W001) 自定义检查:这只是个警告
System check identified 1 issue (0 silenced).
check --fail-level WARNING → rc=1
check --tag lab_tag → rc=0
check --tag models(不含 lab_tag) → rc=0 System check identified no issues (0 silenced).SILENCED_SYSTEM_CHECKS=[lab.W001] → rc=0 System check identified no issues (1 silenced).
标签(Tags.models 或自定义字符串)决定 --tag 能选到它。已知且接受的警告用 SILENCED_SYSTEM_CHECKS 静音。
rc=0;报出的检查 id: ['security.W001', 'security.W002', 'security.W003', 'security.W009', 'security.W012', 'security.W018'] 最后一行: System check identified 6 issues (0 silenced).
register(..., deploy=True) 的检查只在 --deploy 时运行。上线前把它加进 CI。
manage.py check LAB_ERR=1 时 rc=1 被检查拦下 manage.py migrate --check LAB_ERR=1 时 rc=1 被检查拦下 manage.py showmigrations LAB_ERR=1 时 rc=1 被检查拦下 manage.py makemigrations --check --dry-run LAB_ERR=1 时 rc=1 被检查拦下 manage.py test --noinput LAB_ERR=1 时 rc=1 被检查拦下 manage.py help LAB_ERR=1 时 rc=0 没有被拦 manage.py shell -c pass LAB_ERR=1 时 rc=0 没有被拦
需要数据库或依赖完整配置的命令会先检查;help、shell -c 不会。