A CLI tool to map Django model relationships for optimizing queries (select_related vs prefetch_related).
Project description
Django Model Map
Stop guessing your query optimizations.
django-model-map is a simple management command that inspects your Django models and outputs a JSON map of relationships. It explicitly categorizes relations into select_related and prefetch_related candidates, helping you avoid N+1 problems and write optimized QuerySets faster.
Features
- Automatic Classification: Distinguishes between
select_related(ForeignKey, OneToOne) andprefetch_related(ManyToMany, Reverse FK). - Deep Inspection: Supports configurable nesting levels for mapping nested relationships (e.g.,
prefetch_related('lvl1__lvl2__lvl3')). - Reverse Relation Discovery: Finds standard
_setaccessors and customrelated_nameattributes. - Recursion Detection: Identifies self-referencing models.
- GenericForeignKey: Detects GFK fields and marks them in the output.
- JSON / YAML Output: Easy to read, parse, or integrate into other tools.
Installation
pip install django-model-map
# With YAML output support (optional)
pip install django-model-map[yaml]
Add to INSTALLED_APPS:
INSTALLED_APPS = [
...
'django_model_map',
...
]
Usage
CLI
# Inspect all installed apps
python manage.py modelmap
# Inspect a specific app
python manage.py modelmap [app_name]
# Named alternative to positional
python manage.py modelmap --app [app_name]
# Specify inspection nesting level (default: 1)
python manage.py modelmap [app_name] --depth 2
# Exclude models or entire apps (repeatable)
python manage.py modelmap --exclude auth.Permission --exclude auth.Group
# Save to file
python manage.py modelmap [app_name] --output relations.json
# YAML output (requires pip install django-model-map[yaml])
python manage.py modelmap [app_name] --format yaml
Python API
from django_model_map import get_model_map
# Inspect a specific app, depth 2, exclude some models
model_map = get_model_map(app_label='myapp', depth=2, exclude=['myapp.OldModel'])
for model_name, data in model_map.items():
print(data['queryset_snippet'])
Example Output
{
"blog.Post": {
"queryset_snippet": "Post.objects.select_related('author', 'category').prefetch_related('tags', 'comments')",
"select_related_fields": [
"author",
"category"
],
"prefetch_related_fields": [
"tags",
"comments"
],
"details": {
"select_related": [
{
"field_name": "author",
"target_model": "users.User",
"is_recursive": false
},
...
],
"prefetch_related": [
{
"field_name": "tags",
"target_model": "blog.Tag",
"is_recursive": false
},
{
"field_name": "content_object",
"target_model": "Generic",
"is_recursive": false,
"type": "generic"
},
...
]
}
}
}
How it helps
When writing a view, instead of opening models.py and mentally parsing the relationships, just look at the output. With the --depth argument, you can automatically discover deeply nested relationships that need optimization:
- Copy fields from
"queryset_snippet"-> paste into project. - Copy fields from
"select_related_fields"-> paste into.select_related(...). - Copy fields from
"prefetch_related_fields"-> paste into.prefetch_related(...).
Contributing
Pull requests are welcome! For major changes, please open an issue first.
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
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_map-0.4.0.tar.gz.
File metadata
- Download URL: django_model_map-0.4.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01482a1f9bf6292e5cbac0f58fa8e2daa9d911534afb1009ae02d2187e2d4963
|
|
| MD5 |
3d11d49c95d6f9ffcb09f4fb809724ea
|
|
| BLAKE2b-256 |
fcd76aa342325120b7db9bd96d69190b6655e20bb9c63f3d4eb50f959ccee862
|
Provenance
The following attestation bundles were made for django_model_map-0.4.0.tar.gz:
Publisher:
publish.yml on swayll/django-model-map
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_model_map-0.4.0.tar.gz -
Subject digest:
01482a1f9bf6292e5cbac0f58fa8e2daa9d911534afb1009ae02d2187e2d4963 - Sigstore transparency entry: 2085097959
- Sigstore integration time:
-
Permalink:
swayll/django-model-map@a564374fb95fd77fc746c20ded27a6136fd040c4 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/swayll
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a564374fb95fd77fc746c20ded27a6136fd040c4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_model_map-0.4.0-py3-none-any.whl.
File metadata
- Download URL: django_model_map-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8d273e55e8d012740f5fab8227e05455fefd0cb447b000ae7fd89c5726bfdef
|
|
| MD5 |
5871c56479d37283790c68a2afd578e5
|
|
| BLAKE2b-256 |
d45a1807a57dd39c875fcaa2fcc546a5cfb6182e01b7a81387e24e49eba36fb5
|
Provenance
The following attestation bundles were made for django_model_map-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on swayll/django-model-map
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_model_map-0.4.0-py3-none-any.whl -
Subject digest:
b8d273e55e8d012740f5fab8227e05455fefd0cb447b000ae7fd89c5726bfdef - Sigstore transparency entry: 2085097970
- Sigstore integration time:
-
Permalink:
swayll/django-model-map@a564374fb95fd77fc746c20ded27a6136fd040c4 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/swayll
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a564374fb95fd77fc746c20ded27a6136fd040c4 -
Trigger Event:
release
-
Statement type: