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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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