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.
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.
- Python >=3.9
- Django >=3.0
- Django REST Framework >=3.12
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
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")
export interface Path {
name: string;
suffix: string;
suffixes: string[];
stem: string;
isDirectory: boolean;
size: number;
}
There are more examples in test cases.
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) |