Skip to main content

django-unused-model-inspector

django-unused-model-inspector is a Django app that reports model fields and model methods that appear to be unused across registered apps.

The package is intentionally conservative: it reports potentially unused members and includes confidence metadata. It does not delete or rewrite models.

References are tracked per model whenever the scanner can infer the model from querysets, annotations, admin classes, serializers/forms, templates, or runtime evidence. This avoids treating Customer.status as evidence that Order.status is also used.

Installation

Add the app to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_unused_model_inspector",
]

Run the management command:

python manage.py detect_unused_model_members

Useful options:

python manage.py detect_unused_model_members --app billing
python manage.py detect_unused_model_members --format json
python manage.py detect_unused_model_members --format sarif
python manage.py detect_unused_model_members --runtime-evidence runtime-evidence.json
python manage.py detect_unused_model_members --fail-on-findings

Configuration

UNUSED_MODEL_INSPECTOR = {
    "IGNORE_APPS": ["admin", "sessions"],
    "IGNORE_MODELS": ["accounts.User"],
    "IGNORE_FIELDS": ["accounts.User.legacy_external_id"],
    "IGNORE_METHODS": ["billing.Invoice.calculate_total"],
    "IGNORE_PATHS": ["*/migrations/*", "*/tests/*"],
    "SOURCE_PATHS": [],
    "TEMPLATE_PATHS": [],
    "TEMPLATE_EXTENSIONS": [".html", ".txt"],
    "SCAN_TEMPLATES": True,
}

When SOURCE_PATHS is empty, the command scans settings.BASE_DIR. When TEMPLATE_PATHS is empty, the command scans the same paths as SOURCE_PATHS for Django template references.

Runtime evidence

Static analysis cannot see every dynamic Django or Python access pattern. For those cases, wrap representative code or a test run with the runtime recorder:

from django_unused_model_inspector.runtime import record_model_member_access


def test_dynamic_customer_usage():
    with record_model_member_access("runtime-evidence.json", app_labels=["customers"]):
        run_dynamic_customer_flow()

Then merge the evidence into a scan:

python manage.py detect_unused_model_members --runtime-evidence runtime-evidence.json

The recorder monkey-patches model __getattribute__ while the context manager is active, records access to collected model fields, methods, and properties, then restores the original classes on exit.

Release files for django-unused-model-inspector 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-unused-model-inspector 0.1.2
File Size Uploaded
django_unused_model_inspector-0.1.2.tar.gz 13.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-unused-model-inspector 0.1.2
File Interpreter ABI Platform
django_unused_model_inspector-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 28.6 kB

Release files / django_unused_model_inspector-0.1.2.tar.gz

Download URL django_unused_model_inspector-0.1.2.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e0ccbc7a59e0d6fb2afd9c23466a100f4aa135399b647cbbb5dcf8dc2548c872
BLAKE2b-256 checksum
How to use checksums
e0fb2e2acddf0326921eb5f437ff43c717f22e80329ddfade5ff820599db9b51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Release files / django_unused_model_inspector-0.1.2-py3-none-any.whl

Download URL django_unused_model_inspector-0.1.2-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9c360598045b78ea388a2bcd72809216c6f46fc662934199e49c1d60d86c5e8
BLAKE2b-256 checksum
How to use checksums
34427717637c473d1ae95253ebf01082c26266e9aabe37b0dcf91553c0415956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page