Skip to main content

Disable Django database writes.

Project description

Disable Django database writes.


Python 3.5 to 3.8 supported.

Django 2.2 to 3.0 supported.

Deploying a Django project? Testing a Django project? Are your tests slow? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.


Install with pip:

python -m pip install django-read-only

Then add to your installed apps:



Set the environment variable DJANGO_READ_ONLY to anything but the empty string, and all data modification queries will cause an exception:

DJANGO_READ_ONLY=1 python shell
>>> User.objects.create_user(username="hacker", password="hunter2")

You can put this in the shell profile file (bashrc, zshrc, etc.) of the user on your production system. This way developers performing exploratory queries can’t accidentally make changes, but writes remain enabled for non-shell processes like your WSGI server.

During a session with DJANGO_READ_ONLY set, you can re-enable writes for the current thread 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(...)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-read-only-1.0.0.tar.gz (7.5 kB view hashes)

Uploaded source

Built Distribution

django_read_only-1.0.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page