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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_unused_model_inspector-0.1.1.tar.gz.
File metadata
- Download URL: django_unused_model_inspector-0.1.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe47e0e35d63ebaf17be76bda3ca1c0d294de25320cbc3eac4efd3ea9d9f035b
|
|
| MD5 |
a37044af630f01288b0ed86098182d3b
|
|
| BLAKE2b-256 |
416b3d0fa04f5f98f666984c3dedb2206544cdecfc7ead04a4af0cb366dfbc74
|
File details
Details for the file django_unused_model_inspector-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_unused_model_inspector-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
713b945bb9d8df6ff9b11e7eb7d0830626c884b20fc638ad9ec6e2810634ebec
|
|
| MD5 |
3a2ea0a8dcf73a76241ce907509a2fb0
|
|
| BLAKE2b-256 |
134b7b17fe5e50d2a4fc146fb771c8a653a0b124dfacd66f1dbf78fb8dbfc22c
|