Not gonna lie, the Django Admin Panel is super helpful for managing models, data, and users directly from a user-friendly interface. You don’t have to create advanced search and filtration features from scratch—it’s already built-in!
But what if you want to restrict editing for certain models or fields?
Sometimes, allowing editing in the Admin Panel isn’t the best idea, especially when:
- The data needs to remain consistent and shouldn’t be modified accidentally.
- You only want to display data without adding or editing it.
Real-Life Scenario
Recently, one of my clients needed a way to view data with filtration and pagination. The data was scraped from another website, and the client wanted an easy way to track new property listings.
For this, I decided to use Django. It gave me a simple way to manage and display the data without the hassle of creating custom pages.
Take a look at the screenshots:
Admin Panel Overview:
Here, the admin panel only shows Import/Export options. There’s no way to add or edit data from the admin panel.
Individual Page:
The individual entry pages also don’t have any edit options.
Now, let me show you how to set this up in Django.
Steps to Disable Editing
1. Define Your Model
Here’s an example of the model I used:
from django.db import models
class Property(models.Model):
name = models.CharField(max_length=255)
price = models.IntegerField()
location = models.CharField(max_length=255)
date_listed = models.DateField()
def __str__(self):
return self.name
This model is used to store property data scraped from the external website.
2. Customize the Admin Panel
To disable editing, simply override two functions in your admin.py
:
-
has_add_permission
: Disables adding new entries. -
has_change_permission
: Disables editing existing entries.
Here’s how the admin.py
looks:
from django.contrib import admin
from .models import Property
class ReadOnlyAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'location', 'date_listed') # Fields to display
def has_add_permission(self, request):
return False # Disable adding new entries
def has_change_permission(self, request, obj=None):
return False # Disable editing existing entries
admin.site.register(Property, ReadOnlyAdmin)
I’ve also used a base class here to manage list_display
for better data presentation.
What Does This Do?
No Add Option
The “Add” button is removed from the admin panel.No Edit Option
When you click on an entry, it opens in read-only mode, without any save buttons.
This setup is perfect for situations where:
- You only want to display data.
- Editing and adding entries isn’t necessary or could cause issues.
Why Is This Useful?
- Preserve Data Integrity: If the data comes from an external source (like scraping or APIs), it’s better to avoid manual editing in the admin panel to ensure consistency.
-Read-Only Views for Reference Data:
Sometimes, the admin panel is just used for viewing or filtering data. For example, analytics dashboards, property listings, or logs.
Conclusion
Disabling editing in Django Admin is super easy and incredibly useful when you need to protect or display data without allowing accidental changes.
Follow the steps above, and you’ll have a secure, read-only admin panel ready in no time.
Stay Connected - @syedamahamfahim 🐬
Top comments (0)