Django: Custom Middleware for Alerting High Database Querying
In this post I will show you how you can implement a custom middleware that you can use in your development environment to monitor queries to the database in your views when developing Django applications.
Let's begin by creating a middleware.py
file (a nice convention) where we will place our new middleware. The middleware is relatively simple and small, this is how it looks like:
You can customize the threshold value as you see fit.
Finally add the new middleware to the MIDDLEWARE
configuration in your settings:
MIDDLEWARE = [
# Other middleware...
'myapp.middleware.DatabaseQueryAlertingMiddleware',
]