Skip to main content

django-rest-typescript-generator

A TypeScript code generator for Django Rest Framework, which saves your hand-working and guarantees consistency between Python codes and modern frontend codes written in TypeScript.

Features

It generates TypeScript codes from the following Python types.

  • Django REST Framework serializers: manual working on Serializer, ModelSerializer derived from Django ORM models, DataclassSerializer via djangorestframework-dataclasses.
  • Python dataclasses: Classes decorated by dataclasses.dataclass.
  • Python enums: Subclasses of enum.Enum.

It also supports nested types and composite types.

Requirements

  • Python >=3.9
  • Django >=3.0
  • Django REST Framework >=3.12

Usage

Install using pip.

$ pip install django-rest-typescript-generator

Put a tsgconfig.py file with build tasks into your Django project's root.

from django.conf import settings
from django_rest_typescript_generator.build import build

BUILD_DIR = settings.BASE_DIR / "statics/types"

BUILD_TASKS = [
    build(Foo),
    build(BarSerializer, {"alias": "Foobar"}),
]

Add django_rest_typescript_generator to your INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    "django_rest_typescript_generator"
]

Run the buildtypescript command on manage.py.

$ python manage.py buildtypescript

Or you can switch to another place.

$ python manage.py buildtypescript --build-dir /somewhere/you/cannot/explain

Or you can switch to another place.

$ python manage.py buildtypescript --build-dir /somewhere/you/cannot/explain

Examples

Input: Serializer

class PathSerializer(serializers.Serializer):
    name = serializers.CharField()
    suffix = serializers.CharField()
    suffixes = serializers.ListField(child=serializers.CharField())
    stem = serializers.CharField()
    is_directory = serializers.BooleanField(source="is_dir")
    size = serializers.IntegerField(source="stat.st_size")

Output: Interface

export interface Path {
  name: string;
  suffix: string;
  suffixes: string[];
  stem: string;
  isDirectory: boolean;
  size: number;
}

There are more examples in test cases.

Build Options

All options are listed in the table below.

Name Context Value
alias All str
build_dir All str or Path
enforce_uppercase Enum bool (False)

Metadata

Release files for django-rest-typescript-generator 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-rest-typescript-generator 0.0.2
File Size Uploaded Provenance
django_rest_typescript_generator-0.0.2.tar.gz 10.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-rest-typescript-generator 0.0.2
File Interpreter ABI Platform
django_rest_typescript_generator-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / django_rest_typescript_generator-0.0.2.tar.gz

Download URL django_rest_typescript_generator-0.0.2.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3433c4b1756a9a127012af9e5677713a329dba780f3c8e90c799d19668890215
BLAKE2b-256 checksum
How to use checksums
600499641fa2fa4a566277bacf33a2c893f600617bb5529778e0792e5fcd9ba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release files / django_rest_typescript_generator-0.0.2-py3-none-any.whl

Download URL django_rest_typescript_generator-0.0.2-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e89c8bfee294d0dc6292a3d5f769377c1aa418d3c2d4916ba279c3cba9f4558
BLAKE2b-256 checksum
How to use checksums
358cb6418dede386bbe027b811178e05d2b1f13c7bb53843241d69b3e3ae3df5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Security for django-rest-typescript-generator 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page