Skip to main content

A CLI tool to map Django model relationships for optimizing queries (select_related vs prefetch_related).

Project description

Django Model Map

Django CI PyPI version Python Versions 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:

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_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

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

django_model_map-0.3.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_model_map-0.3.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file django_model_map-0.3.2.tar.gz.

File metadata

  • Download URL: django_model_map-0.3.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_model_map-0.3.2.tar.gz
Algorithm Hash digest
SHA256 424f7928741f41544fe4de66a5872b0f9a91b493ddd87b9968a38f0347275274
MD5 a3181f0e779121457137d8683df8c7c9
BLAKE2b-256 c265db0261f3ab8849e1a7cf0c3255671f14a4e98b4646f8d53324321aebefa3

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_model_map-0.3.2.tar.gz:

Publisher: publish.yml on swayll/django-model-map

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_model_map-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_map-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a92ccdefdec29d20d246f7ecfdd91ba3d9083104d37ae4d29650f2dfe565a27e
MD5 f99b3700ef65a583f806738434e929cc
BLAKE2b-256 c1c3d375db99b4500edab1886bd3cfc0e4c39ddb8c27e9b80eae37485c4c979f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_model_map-0.3.2-py3-none-any.whl:

Publisher: publish.yml on swayll/django-model-map

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page