0
0
DjangoHow-ToBeginner · 3 min read

How to Use readonly_fields in Django Admin to Make Fields Read-Only

In Django admin, use the readonly_fields attribute inside your ModelAdmin class to specify fields that should be displayed but not editable. This makes those fields visible but disables editing in the admin form.
📐

Syntax

The readonly_fields attribute is a list or tuple of field names inside a ModelAdmin class. These fields will be shown as read-only in the admin interface.

Example parts:

  • class YourModelAdmin(admin.ModelAdmin): - defines admin options for your model.
  • readonly_fields = ('field1', 'field2') - makes field1 and field2 read-only in the admin form.
python
from django.contrib import admin
from .models import YourModel

class YourModelAdmin(admin.ModelAdmin):
    readonly_fields = ('field1', 'field2')

admin.site.register(YourModel, YourModelAdmin)
💻

Example

This example shows how to make the created_at and updated_at fields read-only in the Django admin. These fields typically store timestamps and should not be edited manually.

python
from django.contrib import admin
from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class ArticleAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at', 'updated_at')

admin.site.register(Article, ArticleAdmin)
Output
In the Django admin, the Article form will show 'created_at' and 'updated_at' fields as read-only text, preventing any edits while allowing other fields like 'title' and 'content' to be changed.
⚠️

Common Pitfalls

Common mistakes when using readonly_fields include:

  • Not including the field names as strings in a list or tuple.
  • Trying to make a field read-only that is not part of the model or admin form.
  • Expecting readonly_fields to hide the field; it only disables editing but still shows the field.

Also, readonly_fields does not prevent changes to the database if you override save methods or use other forms.

python
class WrongAdmin(admin.ModelAdmin):
    # Wrong: field names not in quotes
    readonly_fields = (created_at, updated_at)  # This will cause a NameError

class RightAdmin(admin.ModelAdmin):
    # Correct: field names as strings
    readonly_fields = ('created_at', 'updated_at')
📊

Quick Reference

Tips for using readonly_fields in Django admin:

  • Use a tuple or list of strings naming the fields to make read-only.
  • Readonly fields are visible but not editable in the admin form.
  • Readonly fields can be used for auto-generated or calculated fields.
  • Readonly fields do not prevent changes outside the admin interface.

Key Takeaways

Use readonly_fields in ModelAdmin to make specific fields read-only in Django admin.
readonly_fields must be a list or tuple of field name strings.
Readonly fields show in the admin form but cannot be edited.
Common errors include missing quotes around field names or using non-existent fields.
Readonly_fields does not hide fields or prevent database changes outside admin.