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.1.tar.gz (13.3 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.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_unused_model_inspector-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fe47e0e35d63ebaf17be76bda3ca1c0d294de25320cbc3eac4efd3ea9d9f035b
MD5 a37044af630f01288b0ed86098182d3b
BLAKE2b-256 416b3d0fa04f5f98f666984c3dedb2206544cdecfc7ead04a4af0cb366dfbc74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_unused_model_inspector-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 713b945bb9d8df6ff9b11e7eb7d0830626c884b20fc638ad9ec6e2810634ebec
MD5 3a2ea0a8dcf73a76241ce907509a2fb0
BLAKE2b-256 134b7b17fe5e50d2a4fc146fb771c8a653a0b124dfacd66f1dbf78fb8dbfc22c

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