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.

Real-World Projects

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:

  1. Pin Django major version with >=4.2,<5.0 to stay within an LTS branch
  2. Use extras for framework integrations like sentry-sdk[django]
  3. Add environment markers for deployment-only packages like gunicorn
  4. Separate dev dependencies into [project.optional-dependencies] groups
  5. Set requires-python to 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

Open full tool