Read-only Fields in Admin

A snippet showing how to define read-only fields in Django admin.


# models.py

from django.db import models

class Ticket(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    status = models.CharField(max_length=20, default='open')
    reporter = models.CharField(max_length=120)
    assignee = models.CharField(max_length=120, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title
  

# admin.py

from django.contrib import admin
from .models import Ticket

@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
    list_display = ('title', 'status', 'reporter', 'assignee', 'created_at', 'updated_at')
    readonly_fields = ('reporter', 'created_at', 'updated_at', 'status_badge')
    fields = (
        'title',
        'description',
        'assignee',
        'status',          # still editable; readonly alternative shown via badge
        'status_badge',    # computed read-only display
        'reporter',        # locked after creation
        'created_at',
        'updated_at',
    )

    def status_badge(self, obj):
        return obj.status.upper()
    status_badge.short_description = 'Status (read-only)'
  
Explanation:
  • Put field names (or callables) in readonly_fields to render them as non-editable text in the form.
  • You can include computed read-only values by adding a ModelAdmin method (e.g., status_badge).
  • Use fields to control the form order and mix editable and read-only entries cleanly.
  • Typical read-only picks: timestamps, creator/report fields, IDs, or audit-related values.
  • Category Admin
  • Total Views 515
  • Last Modified 18 December, 2025
  • Tags #admin #readonly #fields #customization
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.