A utility to inspect Django model schemas
Project description
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!
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_model_inspector-1.0.3.tar.gz.
File metadata
- Download URL: django_model_inspector-1.0.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe16338ea732ed415d636ac8f118665d5332a7ccb98c005c4bb260d1918bc05f
|
|
| MD5 |
8a855251a7bbcb76040c35a7fa44329a
|
|
| BLAKE2b-256 |
f19401ddd2e9b7642c95b21f3e18c8d9190a677e432719a9b38bdbd33238b77b
|
File details
Details for the file django_model_inspector-1.0.3-py3-none-any.whl.
File metadata
- Download URL: django_model_inspector-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66e973eea436e12b0305d1c136c78f0ab16056811bb5c7ca3076f0a97a5c59e7
|
|
| MD5 |
d6e4840a3c8cf4861a03fca468ffdec0
|
|
| BLAKE2b-256 |
6aa3bac6e1b4c1ec31c13aa4b9bdc6f2cdaa21bab520ec2bc6d7270b91901573
|