DEV Community

Cover image for How to disable admin editing in the Django Admin Panel
Syeda Maham Fahim
Syeda Maham Fahim

Posted on

How to disable admin editing in the Django Admin Panel

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:

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:

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

I’ve also used a base class here to manage list_display for better data presentation.


What Does This Do?

  1. No Add Option

    The “Add” button is removed from the admin panel.

  2. 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 🐬

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay