Disable Django database writes.
Project description
Disable Django database writes.
Work smarter and faster with my book Boost Your Django DX which covers django-read-only, IPython, and many other tools.
Requirements
Python 3.9 to 3.13 supported.
Django 3.2 to 5.1 supported.
Installation
Install with pip:
python -m pip install django-read-only
Then add to your installed apps:
INSTALLED_APPS = [
...,
"django_read_only",
...,
]
Usage
In your settings file, set DJANGO_READ_ONLY to True and all data modification queries will cause an exception:
$ DJANGO_READ_ONLY=1 python manage.py shell
...
>>> User.objects.create_user(username="hacker", password="hunter2")
...
DjangoReadOnlyError(...)
For convenience, you can also control this with the DJANGO_READ_ONLY environment variable, which will count as True if set to anything but the empty string. The setting takes precedence over the environment variable.
During a session with DJANGO_READ_ONLY set on, you can re-enable writes by calling enable_writes():
>>> import django_read_only
>>> django_read_only.enable_writes()
Writes can be disabled with disable_writes():
>>> django_read_only.disable_writes()
To temporarily allow writes, use the temp_writes() context manager / decorator:
>>> with django_read_only.temp_writes():
... User.objects.create_user(...)
...
Note that writes being enabled/disabled is global state, affecting all threads and asynchronous coroutines.
Recommended Setup
Set read-only mode on in your production environment, and maybe staging, during interactive sessions. This can be done by setting the DJANGO_READ_ONLY environment variable in the shell profile file (bashrc, zshrc, etc.) of the system’s user account. This way developers performing exploratory queries can’t accidentally make changes, but writes will remain enabled for non-shell processes like your WSGI server.
With this setup, developers can also run management commands with writes enabled by setting the environment variable before the command:
$ DJANGO_READ_ONLY= python manage.py clearsessions
Some deployment platforms don’t allow you to customize your shell profile files. In this case, you will need to find a way to detect shell mode from within your settings file.
For example, on Heroku there’s the DYNO environment variable (docs) to identify the current virtual machine. It starts with “run.” for interactive sessions. You can use this to enable read-only mode in your settings file like so:
if os.environ.get("DYNO", "").startswith("run."):
DJANGO_READ_ONLY = bool(os.environ.get("DJANGO_READ_ONLY", "1"))
else:
DJANGO_READ_ONLY = False
IPython Extension
django-read-only also works as an IPython extension for quick access to enable/disable read-only mode. Load it with:
In [1]: %load_ext django_read_only
You can have the extension always load by setting it up to your IPython configuration file:
c.InteractiveShellApp.extensions.append("django_read_only")
When loaded, use the %read_only line magic to disable or enable read-only mode:
In [2]: %read_only off
Write queries enabled.
In [3]: %read_only on
Write queries disabled.
This reduces the amount of typing needed to disable read-only mode.
How it Works
The most accurate way to prevent writes is to connect as a separate database user with only read permission. However, this has limitations - Django doesn’t support modifying the DATABASES setting live, so sessions would not be able to temporarily allow writes.
Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads. It uses a “fail closed” philosophy, so anything unknown will fail, which should be fairly reasonable.
Because django-read-only uses Django database instrumentation, it cannot block queries running through the underlying database connection (accesses through django.db.connection.connection), and it cannot filter operations within stored procedures (which use connection.callproc()). These are very rare in practice though, so django-read-only’s method works well for most projects.
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
File details
Details for the file django_read_only-1.17.0.tar.gz
.
File metadata
- Download URL: django_read_only-1.17.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b762e99187279cb6f3686b1c2f1532aeb7c11c48c78051a5b13c0a2c4303b83 |
|
MD5 | 8d83a8cf3e4558a7178acc1a779fb3fe |
|
BLAKE2b-256 | 366e7063d9861b352e6ccb29cca29ca95a5d7accca89d34084b6d4079183d7f1 |
File details
Details for the file django_read_only-1.17.0-py3-none-any.whl
.
File metadata
- Download URL: django_read_only-1.17.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d9ac357ded00a0cf0cf39a2a212605646958b400ea58d775f5963f9199fb544 |
|
MD5 | df50b61782b6fc065ba5e90e80b57d5b |
|
BLAKE2b-256 | bca576cf7a5f8ba8567ed0abd14bf5c084bc5bc96b41fed46e56d3d1888bec0c |