Say you have a bunch of developers that occasionally need Django shell access to production, but you want this to be an exceptional event. Here is a drop-in replacement for ./manage.py shell that defaults to read-only mode, but lets the developer switch to writable mode on the fly, while notifying the team.
The strategy here is to use Django’s database router mechanism to throw an exception when trying to write to the database.
Install
Drop this into your project as myapp/management/commands/shell.py and it will over-ride the default shell command.
Hipchat
In my case, I’m notifying the team via Hipchat. Of course, you can replace this function with a version that sends out an email, etc. If you’re curious, here is the hipchat code: