首页 > 解决方案 > 忽略黑色格式化程序的 pyproject.toml 文件中的 Django 迁移

问题描述

我刚刚为我的 Django 存储库设置了BlackPre-Commit 。

我使用了我遵循的教程中的 Black 的默认配置,它运行良好,但我无法从中排除我的迁移文件。

这是我一直使用的默认配置:

pyproject.toml

[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

我使用Regex101.com来确保^.*\b(migrations)\b.*$匹配apps/examples/migrations/test.py.

[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
  | ^.*\b(migrations)\b.*$
)/
'''

当我将该正则表达式行添加到我的配置文件并运行pre-commit run --all-files时,它会忽略该.git文件夹,但仍会格式化迁移文件。

标签: pythondjangoregexpre-commit.compyproject.toml

解决方案


将迁移排除添加到您的.pre-commit-config.yaml文件中

- id: black
  exclude: ^.*\b(migrations)\b.*$

推荐阅读