Skip to main content

Export Django models and serializers to TypeScript interfaces

Project description

django-ts-exporter

Export Django models and serializers to TypeScript interfaces.

This tool allows you to easily export Django models and serializers as TypeScript interfaces, enabling better type safety and integration between Django backend and TypeScript frontend applications.

Features

  • Export Django models to TypeScript interfaces.
  • Export Django REST Framework serializers to TypeScript interfaces.
  • Support for various Django field types.
  • Generates TypeScript enums for Django model choices.
  • Supports both models and serializers as the source.
  • Logs any fields that cannot be determined.

Supported Field Types

Models

  • CharField -> string
  • IntegerField -> number
  • BooleanField -> boolean
  • DateTimeField -> string (ISO 8601)
  • DateField -> string (ISO 8601)
  • DecimalField -> number
  • UUIDField -> string
  • JSONField -> { [key: string]: any }
  • ForeignKey -> RelatedModel
  • ManyToManyField -> RelatedModel[]
  • OneToOneField -> RelatedModel
  • FileField -> File
  • Fields with choices -> enum

Serializers

  • CharField -> string
  • IntegerField -> number
  • BooleanField -> boolean
  • DateTimeField -> string (ISO 8601)
  • DateField -> string (ISO 8601)
  • DecimalField -> number
  • PrimaryKeyRelatedField -> RelatedSerializer
  • ManyRelatedField -> RelatedSerializer[]
  • FileField -> File
  • ListSerializer -> RelatedSerializer[]
  • SerializerMethodField -> inferred type or any
  • Fields with choices -> enum

Installation

pip install django-ts-exporter

Usage

Basic Usage

django-ts-exporter --outdir ./typescript --exclude info e2e --logs --source models

Available Commands

django-ts-exporter --help

Outputs:

usage: django-ts-exporter [-h] [--outdir OUTDIR] [--exclude [EXCLUDE ...]] [--logs] [--source {serializers,models}] [-v]

Export Django models and serializers to TypeScript interfaces.

optional arguments:
  -h, --help            show this help message and exit
  --outdir OUTDIR       Output directory for the TypeScript files. (default: ./typescript)
  --exclude [EXCLUDE ...]
                        List of apps to exclude from export. (default: [])
  --logs                Enable detailed logs. (default: False)
  --source {serializers,models}
                        Source to export: serializers or models (default: serializers)
  -v, --version         show program's version number and exit

Example

To export TypeScript interfaces from Django serializers:

django-ts-exporter --outdir ./typescript --exclude info e2e --logs --source serializers

To export TypeScript interfaces from Django models:

django-ts-exporter --outdir ./typescript --exclude info e2e --logs --source models

Running Tests

To run the tests, use the following command:

python -m unittest discover tests

Contributing

Feel free to open issues or submit pull requests on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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-exporter-0.4.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

django_ts_exporter-0.4.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file django-ts-exporter-0.4.0.tar.gz.

File metadata

  • Download URL: django-ts-exporter-0.4.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.18

File hashes

Hashes for django-ts-exporter-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d8b7697081fc367628bc27b37c75c6e37a4af404d126663fa4f958e543e559e5
MD5 63294171fda9f18135c4a6bbef262ee6
BLAKE2b-256 ca901162c5da10934a190ed4a8cce408ee42cbad6fdae915f5069f81e1a92f11

See more details on using hashes here.

File details

Details for the file django_ts_exporter-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ts_exporter-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95086785e975dbabfb7bd901c7311f6749c5bbca3b0aa57aac8ccb999ab34952
MD5 200b533d994c298791dd11148b69b82b
BLAKE2b-256 7dab920a294e785ce4b1e228a6038291ab7f1f6a8a227df86966196eb0dc7729

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page