Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring Django Migrations in pyproject.toml file for Black formatter

I just got Black and Pre-Commit set up for my Django repository.

I used the default config for Black from the tutorial I followed and it's been working great, but I am having trouble excluding my migrations files from it.

Here is the default configuration I've been using:

pyproject.toml

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

I used Regex101.com to make sure that ^.*\b(migrations)\b.*$ matched 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.*$
)/
'''

When I add that regex line to my config file, and run pre-commit run --all-files, it ignores the .git folder but still formats the migrations files.

like image 977
Clark Sandholtz Avatar asked Nov 17 '25 03:11

Clark Sandholtz


1 Answers

Add the migration exclusion to your .pre-commit-config.yaml file

- id: black
  exclude: ^.*\b(migrations)\b.*$
like image 170
Antony Orenge Avatar answered Nov 18 '25 16:11

Antony Orenge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!