How to Display readonly_fields in Admin in Django 


               


How to Display readonly_fields in Admin in Django



Python


In this article, we show how to display readonly_fields in admin in Django.

By default, fields marked as 'editable= False' will not show up in admin.

However, there still is a way that you can display them without changing 'editable= False'

The way to do so is to create a ModelAdmin in which you can display readonly_fields.

So, examples of fields that are readonly_fields are DateTimeField(s).

These fields cannot be seen unless you explicitly write code that makes them seeable.

So this is what we're going to do in this page.

As an example, we'll create a simple database table called MemberComment. This database table will have 3 fields: comment, timestamp (for when the comment was submitted), and updated (for when it was last updated).

This is shown below.

models.py File




By default, timestamp and updated are marked 'editable= False', so they will not show up by default in admin.

So what we have to do is go into the admin.py file and create a ModelAdmin for the database table.

We then can render out readonly_fields.

We can register these fields into admin in the admin.py page, using the following code.



Once you register it, you will see the following output, shown below.

Timestamp and last updated database table fields in Django

And this is how readonly_fields can be displayed in admin in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...