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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4601c3b3189e48cb5f8f5dd7540865a981b9fab7e32dae85f5afb55dcf45f11e
|
|
| MD5 |
98655e20a762c3f966707d064f39a086
|
|
| BLAKE2b-256 |
30d0adead82fc7e30bb2533398efd2193fd0492bc3fc4a2c640327f690e4e866
|
Provenance
The following attestation bundles were made for django_ts-0.1.2.tar.gz:
Publisher:
publish.yml on regiscamimura/django-ts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_ts-0.1.2.tar.gz -
Subject digest:
4601c3b3189e48cb5f8f5dd7540865a981b9fab7e32dae85f5afb55dcf45f11e - Sigstore transparency entry: 906549474
- Sigstore integration time:
-
Permalink:
regiscamimura/django-ts@d967bbaef2ac4ab53370a1d10e59abc71b131d0d -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/regiscamimura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d967bbaef2ac4ab53370a1d10e59abc71b131d0d -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c227d104bee38346b5f5f2b605b4c0207e7deca8d93c12498b951c5b999a5891
|
|
| MD5 |
cdb50980bc0a248fafb51d923ebec999
|
|
| BLAKE2b-256 |
375d65c180cd28d04fe5e5edf2e77b16811b9cdbea3c9d2f12362c516c5a0762
|
Provenance
The following attestation bundles were made for django_ts-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on regiscamimura/django-ts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_ts-0.1.2-py3-none-any.whl -
Subject digest:
c227d104bee38346b5f5f2b605b4c0207e7deca8d93c12498b951c5b999a5891 - Sigstore transparency entry: 906549478
- Sigstore integration time:
-
Permalink:
regiscamimura/django-ts@d967bbaef2ac4ab53370a1d10e59abc71b131d0d -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/regiscamimura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d967bbaef2ac4ab53370a1d10e59abc71b131d0d -
Trigger Event:
release
-
Statement type: