Skip to main content

A tool to export model schemas from Django, SQLAlchemy, and FastAPI to JSON

Project description

Model Schema Exporter

Model Schema Exporter is a versatile Python tool that extracts model schemas from Django, Flask, and FastAPI applications and exports them to JSON format. This tool is particularly useful for developers who need to visualize their data models or integrate them with other tools.

Installation

You can install Model Schema Exporter using pip:

pip install model-schema-exporter

Usage

Model Schema Exporter can be used as a command-line tool:

model-schema-exporter <framework> <output_file> [OPTIONS]
  • <framework>: The web framework you're using. Choose from 'django', 'flask', or 'fastapi'.
  • <output_file>: The path where the output JSON file will be saved.

Options

  • --app-label: (Django only) Specify a particular Django app to extract models from.
  • --project-root PATH: Specify the root directory of your project. If not provided, the current working directory is used as the default.
  • --verbose: Enable verbose logging for more detailed output.

Examples

  1. For a Django project (all apps):
model-schema-exporter django output.json --project-root /path/to/your/project
  1. For a Django project (specific app):
model-schema-exporter django output.json --app-label myapp --project-root /path/to/your/project
  1. For a Flask project:
model-schema-exporter flask output.json --project-root /path/to/your/project
  1. For a FastAPI project:
model-schema-exporter fastapi output.json --project-root /path/to/your/project

How It Works

Model Schema Exporter uses framework-specific shell scripts to extract model information:

  1. extract_django_models.sh: Extracts models from Django projects.
  2. extract_flask_models.sh: Extracts models from Flask projects using SQLAlchemy.
  3. extract_fastapi_models.sh: Extracts Pydantic models from FastAPI projects.

These scripts are executed in the context of your project, ensuring that all project-specific configurations and environment variables are respected.

Project Root

The --project-root option allows you to specify the root directory of your project. This is crucial for the tool to locate your project files and execute the extraction scripts correctly.

  • If not specified, the current working directory is used as the project root.
  • The project root is typically:
    • For Django: The directory containing manage.py
    • For Flask/FastAPI: The directory containing your main application file (e.g., app.py or main.py)

Output Format

The tool generates a JSON file with the following structure:

{
  "models": [
    {
      "name": "ModelName",
      "fields": [
        {
          "name": "field_name",
          "type": "FieldType",
          "nullable": true,
          "unique": false
        }
        // ... more fields
      ]
    }
    // ... more models
  ]
}

Framework-specific Notes

Django

  • Ensure your Django settings are properly configured.
  • You can extract models from all apps or specify a particular app using the --app-label option.

Flask

  • Make sure your models are defined using SQLAlchemy and are importable from your main application file.

FastAPI

  • Ensure your Pydantic models are importable from your main application file.

Troubleshooting

If you encounter issues:

  1. Ensure you're running the command from your project's root directory, or use the --project-root option.
  2. Check that your project's virtual environment is activated.
  3. For Django projects, make sure your DJANGO_SETTINGS_MODULE environment variable is correctly set.
  4. Run the exporter with the --verbose flag for more detailed logging:
model-schema-exporter django output.json --project-root /path/to/your/project --verbose

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

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

model-schema-exporter-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

model_schema_exporter-0.1.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file model-schema-exporter-0.1.3.tar.gz.

File metadata

  • Download URL: model-schema-exporter-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.19 tqdm/4.64.1 importlib-metadata/4.2.0 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for model-schema-exporter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 89b27e8d2465e461e78ad00cea2214be48ae239c9e0e8f59799c477b5583e28c
MD5 32c0569800c97f0a0ee32a9c68232d0a
BLAKE2b-256 187331d8c4752201f47b39b36af8fdcd5634eabfdff44b0836f9c921338557f4

See more details on using hashes here.

File details

Details for the file model_schema_exporter-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: model_schema_exporter-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.19 tqdm/4.64.1 importlib-metadata/4.2.0 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for model_schema_exporter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7f7d86533dee94b14dfb94326d70cc75d64dde227910929185ea5efc709c9132
MD5 795663b356e93bc22818bd2c56dab882
BLAKE2b-256 75858cebf6b8f3e5148e8755841648d820703628dd2c7cf3ca27b648106bbe4f

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