Skip to main content

Generate Django code from OpenAPI documents.

Project description

OpenAPI to Django

Python 3.11 OpenAPI 3.1 Django 5.1 MIT License

Note that this project is currently a pre-release so changes are likely to occur.

Please use the repository's GitHub Discussions to report bugs, suggest features and ask any questions. Adding bug reports and feature requests as GitHub Issues may be supported in the future.

Overview

OpenAPI to Django is a Python tool used to generate Django code from OpenAPI documents.

While popular tools like FastAPI can generate OpenAPI documents from Python code, there are fewer tools available to do the opposite, generating a backend server from an OpenAPI document. OpenAPITools' OpenAPI Generator includes generating server stubs from OpenAPI documents, but it currently doesn't support Django (as of 30th May 2025), so I decided I'd give it a go!

Setup

Installing with pipx

Use pipx install openapi_to_django to install OpenAPI to Django from PyPI. Instructions for installing pipx can be found here.

Building from Source

The package can also be built from source. This requires Poetry to be installed, use pipx install poetry or see instructions here.

# clone the repository
git clone git@github.com:jaytik1/openapi_to_django.git
cd openapi_to_django/

# use Poetry to install dependencies
poetry install

# build the package (output in the dist/ folder)
poetry build

Using the Tool

Once installed, use the openapi_to_django command line tool to generate Django code. The tool requires an OpenAPI document to be specified, which can be either a JSON or YAML file, examples of which are in the openapi/ folder. Custom templates can also be passed as inputs to be used by the tool.

Currently, Django urls.py and views.py files are generated from the specified OpenAPI document. The tool can be used in either "projects" or "files" mode which are described below.

Projects Mode

Using "projects mode" (with the projects keyword) creates a full Django project with the generated files inside. Additional command line arguments for configuring the generated project can also be given. Below are examples of how projects mode can be used.

# view all arguments available in projects mode
openapi_to_django projects --help

# create a new project using the default variables
openapi_to_django projects openapi/example.openapi.json

# create a new project with specified project and app names
openapi_to_django projects openapi/example.openapi.json \
  --project-name example_project \
  --app-name example_app

# create a new project using custom templates
openapi_to_django projects openapi/example.openapi.json \
  --urls-template src/openapi_to_django/templates/urls.py-tpl \
  --views-template src/openapi_to_django/templates/views.py-tpl \
  --project-template src/openapi_to_django/templates/project_template/ \
  --app-template src/openapi_to_django/templates/app_template/

Files Mode

Using "files mode" (with the files keyword) just outputs the generated urls.py and views.py files without creating a full Django project. The locations for these files can be specified, and if these files already exist, the generated code is appended to the end of the files. Below are examples of how files mode can be used.

# view all arguments available in files mode
openapi_to_django files --help

# generate files using the default variables
openapi_to_django files openapi/example.openapi.json

# output files to a custom location
openapi_to_django files openapi/example.openapi.json \
  --urls-target urls.py \
  --views-target views.py

# generate files using custom templates
openapi_to_django files openapi/example.openapi.json \
  --urls-template src/openapi_to_django/templates/urls.py-tpl \
  --views-template src/openapi_to_django/templates/views.py-tpl

FAQs

What are template files and how do I use them?

  • Template files have the extension .py-tpl and can be found in the templates/ folder
  • They are used to render files in a specific way and are written in the Django template language
    • As well as in this project, they are used by Django's built-in startproject and startapp commands
  • urls.py-tpl and views.py-tpl are the key files, as they determine how OpenAPI paths are converted to Django paths and view functions
  • Custom templates can be passed to the tool using the appropriate arguments

Licensing

  • Licenses for the project can be found in LICENSES/
  • LICENSE.txt: This repository is distributed under the MIT License.
  • LICENSE_DJANGO.txt: As elements of the Django source code have been used (including project and app templates, as well as some template rendering systems), the Django BSD 3-Clause License file is also included.

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

openapi_to_django-0.1.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_to_django-0.1.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file openapi_to_django-0.1.0.tar.gz.

File metadata

  • Download URL: openapi_to_django-0.1.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for openapi_to_django-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8edc993e71daa0d75b93ec437e6f840b4d28f7a51fd2ab41fd98756261c6d89a
MD5 4e911ef160ed2cc0d673049eb9084715
BLAKE2b-256 d450f5ae68e847de29d11c430c2b6b19b3abf19e2ec4caf3ec1ce67ea1903cc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_to_django-0.1.0.tar.gz:

Publisher: publish.yml on jaytik1/openapi_to_django

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openapi_to_django-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_to_django-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f53eeff78852aca5572839a4d784a93744b172eeab6b18e6b841e1c86057272e
MD5 9dbc0d6f70f837a8cce4d48e45d8ba2e
BLAKE2b-256 b255f56c695f50800f8a20b81d5db03670841700a0478145c6e0ac9c78035f05

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_to_django-0.1.0-py3-none-any.whl:

Publisher: publish.yml on jaytik1/openapi_to_django

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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