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
- For a Django project (all apps):
model-schema-exporter django output.json --project-root /path/to/your/project
- For a Django project (specific app):
model-schema-exporter django output.json --app-label myapp --project-root /path/to/your/project
- For a Flask project:
model-schema-exporter flask output.json --project-root /path/to/your/project
- 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:
extract_django_models.sh
: Extracts models from Django projects.extract_flask_models.sh
: Extracts models from Flask projects using SQLAlchemy.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
ormain.py
)
- For Django: The directory containing
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:
- Ensure you're running the command from your project's root directory, or use the
--project-root
option. - Check that your project's virtual environment is activated.
- For Django projects, make sure your
DJANGO_SETTINGS_MODULE
environment variable is correctly set. - 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
Built Distribution
File details
Details for the file model-schema-exporter-0.1.2.tar.gz
.
File metadata
- Download URL: model-schema-exporter-0.1.2.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | d76d7df604a344d5f9b60219a6927fefe4733e56c69f9da2991b13835284815b |
|
MD5 | e43459c21e75d4d08d788aef9e3b562c |
|
BLAKE2b-256 | 96fc8bc1b6d5f8fcbd77f9e65cf14096b3836ebe8fa8597c2f7092ec192c289d |
File details
Details for the file model_schema_exporter-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: model_schema_exporter-0.1.2-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ef14e6850bd4382b5b81a604368ddb4ea07977d92039c362dd2ed8db124bdcb |
|
MD5 | 452435a361848ef3c76fe337fafc3088 |
|
BLAKE2b-256 | b492d10a07ce0eff40c187daba13528c94ddf30c8bb5d368d0515490dcf8ded3 |