Django Schema Inspector
Django Schema Inspector is a utility designed to inspect and expose metadata about Django models in a structured format. It provides a simple, configurable way to retrieve schema information for models across Django applications, with optional filtering by user-defined settings.
Features
- Schema Collection: Gathers metadata for all Django models, including field types, relationships, and defaults.
- Configurable Filtering: Optionally exclude built-in Django apps, private fields, or specific fields.
- JSON Output: Exposes schema data via a Django view for easy JSON retrieval.
Installation
Install Django Schema Inspector via pip:
pip install django-schema-collector
Configuration
In your Django settings.py, add optional settings prefixed with DMI_ to customize the behavior of the schema collector. Available settings:
DMI_INCLUDE_PRIVATE_FIELDS: Set toTrueto include private fields in the schema output. Default isFalse.DMI_INCLUDE_METHODS: Set toTrueto include model methods in the schema output. Default isFalse.DMI_EXCLUDE_FIELDS: A list of field names to exclude from the output. Default is an empty list ([]).DMI_APP_LABELS: A list of specific app labels to include in the schema output. Default isNone, which includes all apps.DMI_USER_DEFINED_ONLY: Set toTrueto exclude built-in Django apps from the output. Default isFalse.
Example configuration:
# settings.py
DMI_INCLUDE_PRIVATE_FIELDS = False
DMI_INCLUDE_METHODS = False
DMI_EXCLUDE_FIELDS = ['password', 'email']
DMI_APP_LABELS = ['myapp', 'another_app']
DMI_USER_DEFINED_ONLY = True
Usage
1. Schema Collection
The main component of this SDK is the DjangoSchemaInspector class, which provides methods to collect schema metadata.
from dmi.inspector import DjangoSchemaInspector
# Initialize the collector
collector = DjangoSchemaInspector()
# Collect schema information
schema = collector.collect_schema()
print(schema)
2. Exposing Schema as JSON
If you want to expose your schema in a endpoint, create a url path in urls.py and map the view. Or, you can import a pre-defined SchemaView and map to a specific path
# urls.py
from django.urls import path
from dmi.views import SchemaView
urlpatterns = [
path('api/schema/', SchemaView.as_view(), name='schema-view'),
]
Accessing /api/schema/ will return a JSON representation of the schema.
Example Response
A sample JSON response from /api/schema/ endpoint might look like:
{
"myapp": {
"MyModel": {
"id": {"type": "AutoField", "null": false, "blank": false, "max_length": null, "default": null},
"name": {"type": "CharField", "null": false, "blank": false, "max_length": 255, "default": ""},
"created_at": {"type": "DateTimeField", "null": true, "blank": true, "max_length": null, "default": null},
"related_model": {
"type": "ForeignKey",
"relationship": "ForeignKey",
"related_model": {"app": "related_app", "model": "RelatedModel"},
"on_delete": "CASCADE"
}
}
}
}
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please submit a pull request or open an issue for suggestions or feature requests.
Contact
For any issues, please open an issue on GitHub or contact speak2mowzli@gmail.com.
Happy coding!
Metadata
Release files for django-model-inspector 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_model_inspector-1.0.3.tar.gz | 5.0 kB | Without provenance | Details for django_model_inspector-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_model_inspector-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for django_model_inspector-1.0.3-py3-none-any.whl |
Total release size: 13.0 kB
Release files / django_model_inspector-1.0.3.tar.gz
| Download URL | django_model_inspector-1.0.3.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe16338ea732ed415d636ac8f118665d5332a7ccb98c005c4bb260d1918bc05f
|
|
BLAKE2b-256 checksum How to use checksums |
f19401ddd2e9b7642c95b21f3e18c8d9190a677e432719a9b38bdbd33238b77b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / django_model_inspector-1.0.3-py3-none-any.whl
| Download URL | django_model_inspector-1.0.3-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
66e973eea436e12b0305d1c136c78f0ab16056811bb5c7ca3076f0a97a5c59e7
|
|
BLAKE2b-256 checksum How to use checksums |
6aa3bac6e1b4c1ec31c13aa4b9bdc6f2cdaa21bab520ec2bc6d7270b91901573
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Security for django-model-inspector 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.