If you’ve ever feared accidentally writing to your production database – you’re not alone. Today, I’m sharing a game-changing Django package I recently discovered.
It’s like a seatbelt for your database: lightweight, powerful, and a true life-saver in production environments. Once you start using it, you’ll wonder how you ever lived without it.
Explore the implementation here:
👉 GitHub – Rahulbeniwal26119/intermediate-django
To set up your Django project, follow this detailed guide:
📖 The Ultimate Guide to Django Setup Using Docker and Compose
Install the package using pip
or uv
:
pip install django-read-only
Update your settings.py
:
INSTALLED_APPS = [
...
"django_read_only",
...
]
DJANGO_READ_ONLY = True
Now your Django server will run in read-only mode. Let’s test it:
>>> first_user = User.objects.first()
>>> first_user.first_name = 'Rahul'
>>> first_user.save()
# DjangoReadOnlyError: Write queries are currently disabled.
# Enable with django_read_only.enable_writes().
Enabling read-only mode globally will lock your entire app – not ideal for production! Users won’t be able to perform any write actions on your website.
A better alternative: limit read-only mode to just the Django shell.
DJANGO_READ_ONLY = False
in settings.py
.DJANGO_READ_ONLY=1 python manage.py shell
Now test it again:
>>> from django.contrib.auth.models import User
>>> user = User.objects.first()
>>> user.first_name = "Rahul"
>>> user.save()
# DjangoReadOnlyError: Write queries are currently disabled.
Already inside a shell but want to toggle read-only without restarting?
>>> from django.contrib.auth.models import User
>>> user = User.objects.first()
>>> user.save() # This works for now
>>> %load_ext django_read_only
>>> %read_only on # Turns on read-only mode
>>> user.save()
# DjangoReadOnlyError: Write queries are currently disabled.
💡 This is a super useful technique when debugging live issues on production.
I found django-read-only
incredibly helpful, especially when you need to debug production safely without risking accidental writes or deletes.
This little tool can prevent major headaches – definitely a must-have for serious Django developers.
Drop a 👏 and follow Rahul Beniwal for more deep-dive Django and Python content.