edx-django-utils
edx-django-utils provides a collection of utility functions, middleware, and helpers for developing Django applications within the Open edX ecosystem. It offers features like monitoring integration, plugin management, and common middleware components. The current version is 8.0.1, and it maintains an active development cycle with frequent patch, minor, and occasional major releases addressing new features, bug fixes, and dependency updates.
Common errors
-
ModuleNotFoundError: No module named 'newrelic'
cause Upgrading to `edx-django-utils>=8.0.0` removed `newrelic` from its direct dependencies, but your project still expects it.fixAdd `newrelic` explicitly to your project's `requirements.txt` or `pyproject.toml` and reinstall your dependencies (e.g., `pip install newrelic`). -
ImportError: cannot import name 'RequestCacheMiddleware' from 'edx_django_utils.middleware.request'
cause The `RequestCacheMiddleware` class was deprecated and removed/refactored in `edx-django-utils>=7.2.0`.fixRemove `RequestCacheMiddleware` from your Django `MIDDLEWARE` settings. Check the `edx-django-utils` changelog for suitable alternatives if its functionality is still required. -
raise UnsupportedPythonVersion(f"Unsupported Python version: {sys.version_info}") # pragma: no covercause `edx-django-utils>=6.0.0` dropped support for Python 3.8.fixUpgrade your Python environment to 3.9 or higher. For example, use Python 3.9, 3.10, or 3.11.
Warnings
- breaking Python 3.8 support was dropped in `edx-django-utils==6.0.0`.
- breaking The `newrelic` package was removed as a direct dependency in `edx-django-utils==8.0.0`. If your project relied on `edx-django-utils` to implicitly install `newrelic` for monitoring, you will need to add it to your project's `requirements.txt` explicitly.
- deprecated Several middleware classes, such as `RequestCacheMiddleware`, were deprecated and subsequently removed or refactored in `v7.2.0`.
- gotcha Django 5.2 support was explicitly added in `edx-django-utils==7.3.0`. While earlier versions might work, full compatibility and optimal behavior with Django 5.2 are ensured from this version onwards.
Install
-
pip install edx-django-utils
Imports
- MonitoringMiddleware
from edx_django_utils.monitoring.middleware import MonitoringMiddleware
- set_monitoring_transaction_name
from edx_django_utils.monitoring.utils import set_monitoring_transaction_name
- IdentityContextMiddleware
from edx_django_utils.middleware.identity import IdentityContextMiddleware
Quickstart
# In your Django project's settings.py
INSTALLED_APPS = [
# ... other apps
'edx_django_utils.plugins', # Required for some signals/plugin management features
# ...
]
MIDDLEWARE = [
'edx_django_utils.monitoring.middleware.MonitoringMiddleware',
'edx_django_utils.middleware.identity.IdentityContextMiddleware',
# ... other middleware
]
# Example usage in a view or management command
from django.http import HttpResponse
from edx_django_utils.monitoring.utils import set_monitoring_transaction_name
def my_app_view(request):
set_monitoring_transaction_name('my_app.view_name')
# Your view logic
return HttpResponse('Response from my app!')