Metadata-Version: 2.4
Name: django-model-map
Version: 0.3.2
Summary: A CLI tool to map Django model relationships for optimizing queries (select_related vs prefetch_related).
Home-page: https://github.com/swayll/django-model-map/
Author: Nikolay Fedorov
Author-email: 40500428+swayll@users.noreply.github.com
License: MIT
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=3.2
Dynamic: license-file

# Django Model Map
[![Django CI](https://github.com/swayll/django-model-map/actions/workflows/django.yml/badge.svg)](https://github.com/swayll/django-model-map/actions/workflows/django.yml)
[![PyPI version](https://img.shields.io/pypi/v/django-model-map.svg)](https://pypi.org/project/django-model-map/)
[![Python Versions](https://img.shields.io/pypi/pyversions/django-model-map.svg)](https://pypi.org/project/django-model-map/)
[![License](https://img.shields.io/pypi/l/django-model-map.svg)](https://github.com/swayll/django-model-map/blob/main/LICENSE)

**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) and `prefetch_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 `_set` accessors and custom `related_name` attributes.
- **Recursion Detection**: Identifies self-referencing models.
- **JSON Output**: Easy to read, parse, or integrate into other tools.

## 📦 Installation

Install via pip:

```bash
pip install django-model-map
```
Add it to your `INSTALLED_APPS` in `settings.py`:

```python
INSTALLED_APPS = [
    ...
    'django_model_map',
    ...
]
```

## 🛠 Usage

```bash
# 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:
```json
{
    "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": [...]
        }
    }
}
```
## 💡 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 to discuss what you would like to change.

## 📄 License
[MIT](https://github.com/swayll/django-model-map/tree/main#MIT-1-ov-file)

