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.2.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.2-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_ts-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 4601c3b3189e48cb5f8f5dd7540865a981b9fab7e32dae85f5afb55dcf45f11e
MD5 98655e20a762c3f966707d064f39a086
BLAKE2b-256 30d0adead82fc7e30bb2533398efd2193fd0492bc3fc4a2c640327f690e4e866

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_ts-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.2 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c227d104bee38346b5f5f2b605b4c0207e7deca8d93c12498b951c5b999a5891
MD5 cdb50980bc0a248fafb51d923ebec999
BLAKE2b-256 375d65c180cd28d04fe5e5edf2e77b16811b9cdbea3c9d2f12362c516c5a0762

See more details on using hashes here.

Provenance

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