Converting a Django Project's Dependencies
Complete example of converting a Django web application's requirements.txt to pyproject.toml, including database drivers, REST framework, Celery, and deployment dependencies.
Detailed Explanation
Django projects typically have a rich set of dependencies spanning the web framework, database drivers, caching, task queues, and deployment tools. Here's a complete conversion example.
Django requirements.txt:
# Core
Django>=4.2,<5.0
djangorestframework>=3.14
django-cors-headers~=4.3
django-filter>=23.3
# Database
psycopg2-binary>=2.9
django-redis>=5.4
# Task Queue
celery[redis]>=5.3
django-celery-beat>=2.5
# Auth
djangorestframework-simplejwt>=5.3
django-allauth>=0.58
# Deployment
gunicorn>=21.2 ; sys_platform != "win32"
whitenoise>=6.6
sentry-sdk[django]>=1.39
Converted to pyproject.toml:
[project]
name = "my-django-app"
version = "1.0.0"
requires-python = ">=3.10"
dependencies = [
"Django>=4.2,<5.0",
"djangorestframework>=3.14",
"django-cors-headers~=4.3",
"django-filter>=23.3",
"psycopg2-binary>=2.9",
"django-redis>=5.4",
"celery[redis]>=5.3",
"django-celery-beat>=2.5",
"djangorestframework-simplejwt>=5.3",
"django-allauth>=0.58",
"gunicorn>=21.2 ; sys_platform != 'win32'",
"whitenoise>=6.6",
"sentry-sdk[django]>=1.39",
]
Best practices for Django in pyproject.toml:
- Pin Django major version with
>=4.2,<5.0to stay within an LTS branch - Use extras for framework integrations like
sentry-sdk[django] - Add environment markers for deployment-only packages like
gunicorn - Separate dev dependencies into
[project.optional-dependencies]groups - Set
requires-pythonto match Django's minimum supported Python version
Comments from requirements.txt are preserved during conversion, though TOML comments use the same # syntax. The structural grouping by category (Core, Database, etc.) can be maintained through inline comments in the TOML array.
Use Case
Modernizing a production Django application by migrating from requirements.txt to pyproject.toml as part of adopting hatch or pdm for build management.
Try It — Requirements ↔ Pyproject
Related Topics
Converting a Flask Project's Dependencies
Real-World Projects
Basic requirements.txt to pyproject.toml
Getting Started
Dependencies with Extras (Optional Features)
Advanced Features
Environment Markers (Platform-Specific Dependencies)
Advanced Features
Development Dependencies and Optional Groups
Advanced Features