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

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 
cd django-ts

# 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.4.tar.gz (8.4 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.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_ts-0.1.4.tar.gz
Algorithm Hash digest
SHA256 91eeb202a97631e75520da2d1b7d3a9bf1d9bf303bbd4a727d35c8bfae9bdf5b
MD5 94fe30a431cc93b166a182399658fa82
BLAKE2b-256 4d300ac7f68a81b01e53b42328748a525173c8f52212ef4742e647c591918795

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_ts-0.1.4.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.4-py3-none-any.whl.

File metadata

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

File hashes

Hashes for django_ts-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 963ecb6bf62aab4edab649e1df73ef1cc9035ad579448db4a03384c481ba79a0
MD5 95742675aa916972b23f1c4bcee632de
BLAKE2b-256 ac5be8c6e49d61bc955293c4fb077c542c7a75fa7b7ab0f257ef055d36d3786b

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_ts-0.1.4-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