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.
- JSON Output: Easy to read, parse, or integrate into other tools.
📦 Installation
Install via pip:
pip install django-model-map
Add it to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'django_model_map',
...
]
🛠 Usage
# Inspect all installed apps
python manage.py modelmap
# Inspect a specific app
python manage.py modelmap [app_name]
# Save to file for reference using stdout
python manage.py modelmap [app_name] > relations.json
# or export to file with command
python manage.py modelmap [app_name] [--output[-o]] relations.json
# Specify inspection nesting level (default: 1)
python manage.py modelmap [app_name] --depth 2
📖 Example Output
For a blog application with Post, User, Tag and Comment models:
{
"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_objects": [
{
"field_name": "author",
"target_model": "users.User",
"is_recursive": false
},
...
],
"prefetch_objects": [...]
}
}
}
💡 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"-> paste into.select_related(...). - Copy fields from
"prefetch_related"-> paste into.prefetch_related(...).
🤝 Contributing
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
📄 License
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_map-0.3.0.tar.gz.
File metadata
- Download URL: django_model_map-0.3.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
772698242293b8593307798df62aaac7c431c300e71f83a1bc123bce532f367b
|
|
| MD5 |
f623ced416c954289f789b4f0e17e1fa
|
|
| BLAKE2b-256 |
89892fd51f7e67f2431f16df7099b508b35de97a797b5b2e8230491c267cef77
|
Provenance
The following attestation bundles were made for django_model_map-0.3.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.3.0.tar.gz -
Subject digest:
772698242293b8593307798df62aaac7c431c300e71f83a1bc123bce532f367b - Sigstore transparency entry: 980897452
- Sigstore integration time:
-
Permalink:
swayll/django-model-map@fab2487a5fbad64a9f1343246eb3ba6e05600373 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/swayll
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fab2487a5fbad64a9f1343246eb3ba6e05600373 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_model_map-0.3.0-py3-none-any.whl.
File metadata
- Download URL: django_model_map-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb0b72b486b993b59f42cc5cf766b8a8c6e92d61d50b342e2a4d5af8ecae2608
|
|
| MD5 |
90a541c2044f204e340f538f78bc73a4
|
|
| BLAKE2b-256 |
aa7f5fba771d0610fbac2725bafeb65c56c139327f43f5f9c1d67a477718cece
|
Provenance
The following attestation bundles were made for django_model_map-0.3.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.3.0-py3-none-any.whl -
Subject digest:
cb0b72b486b993b59f42cc5cf766b8a8c6e92d61d50b342e2a4d5af8ecae2608 - Sigstore transparency entry: 980897515
- Sigstore integration time:
-
Permalink:
swayll/django-model-map@fab2487a5fbad64a9f1343246eb3ba6e05600373 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/swayll
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fab2487a5fbad64a9f1343246eb3ba6e05600373 -
Trigger Event:
release
-
Statement type: