Skip to main content

Generate TypeScript constants and enums from Django model

Project description

django-ts

Generate TypeScript constants and enums from Django models. No more copy-pasting values between backend and frontend.

Why?

You've got Django models with constants:

class Order(models.Model):
    STATUS_PENDING = "pending"
    STATUS_CONFIRMED = "confirmed"
    STATUS_SHIPPED = "shipped"

    STATUS_CHOICES = [
        (STATUS_PENDING, "Pending"),
        (STATUS_CONFIRMED, "Confirmed"),
        (STATUS_SHIPPED, "Shipped"),
    ]

    status = models.CharField(max_length=20, choices=STATUS_CHOICES)

And you need the same values in TypeScript. You could copy them manually, forget to update one side, ship a bug, debug for an hour, then remember why you hate manual syncing.

Or just run:

python manage.py synctypes

What you get

constants.ts

export const Order = {
  STATUS_CONFIRMED: "confirmed",
  STATUS_PENDING: "pending",
  STATUS_SHIPPED: "shipped",
} as const

enums.ts

export enum OrderStatusEnum {
  STATUS_PENDING = "pending", // Pending
  STATUS_CONFIRMED = "confirmed", // Confirmed
  STATUS_SHIPPED = "shipped", // Shipped
}

index.ts

export * from "./constants";
export * from "./enums";

Requirements

  • Python 3.10+
  • Django 4.0+

Installation

pip install django-ts-constants

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_ts_constants",
]

Usage

# Generate to ./frontend/constants (default)
python manage.py synctypes

# Custom output directory
python manage.py synctypes --output ./frontend/src/generated

# Check mode - exits 1 if files are outdated (for CI)
python manage.py synctypes --check

# Dry run - see what would change without writing
python manage.py synctypes --dry-run

What gets exported

The command scans your project's Django apps and extracts:

  • Uppercase constants with underscores (STATUS_PENDING = "pending")
    • Strings, integers, floats
  • Choice tuples (STATUS_CHOICES = [(value, label), ...])
    • Become TypeScript enums with labels as comments

Third-party packages are ignored. Only your project's apps are scanned.

CI integration

# GitHub Actions
- name: Check TypeScript constants are in sync
  run: python manage.py synctypes --check

If someone updates a Django constant but forgets to regenerate the TypeScript files, CI fails. No more "works on my machine" surprises.

Development

git clone https://github.com/regiscamimura/django-ts-constants
cd django-ts-constants

# Setup
uv venv && uv pip install -e ".[dev]"

# Test
pytest

# Lint
ruff check . && ruff format --check .

License

MIT


Built by Regis Camimura

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ts-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ts-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file django_ts-0.1.0.tar.gz.

File metadata

  • Download URL: django_ts-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_ts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 609e3941250b39c666f75bf0ea1732843647b0ea9929d2adfc85974012aaf113
MD5 aec99b14dac71c5314991c9bed9ac02d
BLAKE2b-256 0a3018a742f9d369020f3c711d0367a0a62e9373b95d71c80280d4f996a20edb

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_ts-0.1.0.tar.gz:

Publisher: publish.yml on regiscamimura/django-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_ts-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_ts-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_ts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4b79be9d103f7168a6c394e7a2f0844623cb175f40f54e27e18959990aa7fb7
MD5 9e103329e234f31807893299d2d11f0a
BLAKE2b-256 99d2ef7d72f0d528dacb175d3073cd7120c96d435b076a8f7b4ec9bf28c7d022

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_ts-0.1.0-py3-none-any.whl:

Publisher: publish.yml on regiscamimura/django-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page