Skip to content
Notifications
Clear all

Best formatting tool for a Python/Django project

1 Posts
1 Users
0 Reactions
1 Views
(@benchmark_hunter)
Estimable Member
Joined: 4 months ago
Posts: 105
Topic starter   [#15834]

I'm currently optimizing the CI/CD pipeline for a large Django monorepo and need to settle on a single, reliable code formatter. The goal is deterministic formatting across all developer machines and in pre-commit hooks. The main contenders are **Black**, **Ruff format**, and **Prettier** (via its Python plugin).

My baseline requirements are:
* Zero configuration for basic Django patterns (models, views, URLs).
* Compatibility with `isort` for import sorting, or a built-in alternative.
* Speed, as this will run in pre-commit and on every push.

I ran a quick benchmark on our codebase (~1500 `.py` files) using a warmed cache:

| Tool | Command | Avg. Run Time (s) |
|--------------|----------------------------------|-------------------|
| Black | `black --check .` | 4.2 |
| Ruff format | `ruff format --check .` | 1.8 |
| Prettier | `prettier --check .` | 12.7 |

**Raw performance data** clearly favors Ruff. However, I'm more concerned with output stability and Django convention handling. Black is the de facto standard, but Ruff's formatter is gaining parity and offers integrated linting.

My specific points of comparison:
* Handling of long chained method calls in Django ORM queries.
* Formatting of HTML-in-strings within Django templates (`.py` files).
* Default line length and its impact on common patterns.

Example code block I'm using for testing:

```python
# How do they handle this common ORM pattern?
queryset = MyModel.objects.filter(user=request.user).annotate(total_count=Count('related_items')).select_related('profile').order_by('-created_at')
```

Which tool are you using in production for Django projects, and have you encountered any edge cases where the formatter breaks template readability or deviates from Django community style?


Numbers don't lie


   
Quote