Skip to main content

Disable Django database writes.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/django-read-only/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/django-read-only.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

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.14 supported.

Django 4.2 to 6.0 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.

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


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.21.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_read_only-1.21.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file django_read_only-1.21.0.tar.gz.

File metadata

  • Download URL: django_read_only-1.21.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_read_only-1.21.0.tar.gz
Algorithm Hash digest
SHA256 5ffc75ccc8a9d9b7b587b074681472fd8edc49624cf0f537812d839a993182c4
MD5 b722b48c578a4938af45fa90e23121c8
BLAKE2b-256 af893ae55b3f07153a14349a8ce81d2ea4363c339c7fd5d969fee8de58729f8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_read_only-1.21.0.tar.gz:

Publisher: main.yml on adamchainz/django-read-only

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_read_only-1.21.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_read_only-1.21.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77c7d79f013e89ad080527101f4558f674764c43d8b9042f81f0d05d9cdd5db1
MD5 b69c30226ff3ee7eec8f51e3d5c8801a
BLAKE2b-256 266336e67be6a17ba907e50b55d90225265b52b2792029c700c3122e1c2b5d74

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_read_only-1.21.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/django-read-only

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page