Skip to main content

Detect potentially unused Django model fields and methods.

Project description

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.

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_unused_model_inspector-0.1.2.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

django_unused_model_inspector-0.1.2-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file django_unused_model_inspector-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_unused_model_inspector-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e0ccbc7a59e0d6fb2afd9c23466a100f4aa135399b647cbbb5dcf8dc2548c872
MD5 385c1a8115583bdd3efb495f2427767d
BLAKE2b-256 e0fb2e2acddf0326921eb5f437ff43c717f22e80329ddfade5ff820599db9b51

See more details on using hashes here.

File details

Details for the file django_unused_model_inspector-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_unused_model_inspector-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9c360598045b78ea388a2bcd72809216c6f46fc662934199e49c1d60d86c5e8
MD5 01839b0942d2dacf5c0f55cf5d79f01c
BLAKE2b-256 34427717637c473d1ae95253ebf01082c26266e9aabe37b0dcf91553c0415956

See more details on using hashes here.

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