👋
Welcome to my blog!

Django show readonly field

Add readonly field in Django admin, django does not show readonly fields by default.

Django show readonly field
Django

Published At

3/25/2023

Reading Time

~ 1 min read

As seen in Add created_at and updated_at in base class, we can add automatic date and time fields in Django models. But, Django does not show these fields in admin by default. We need to add them manually.

Add readonly field in Django admin

python
class BaseAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at', 'updated_at')
 
		class Meta:
		    abstract = True
python
class BaseAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at', 'updated_at')
 
		class Meta:
		    abstract = True

and then add this class in admin.py

python
class MyModelAdmin(BaseAdmin):
    pass
python
class MyModelAdmin(BaseAdmin):
    pass

register the model with this admin class.

python
admin.site.register(MyModel, MyModelAdmin)
python
admin.site.register(MyModel, MyModelAdmin)

🐣🐣🐣

Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.
Abhishek 🙏

Join My Exclusive Newsletter Community

Step into a world where creativity intersects with technology. By subscribing, you'll get a front-row seat to my latest musings, full-stack development resources, and exclusive previews of future posts. Each email is a crafted experience that includes:

  • In-depth looks at my covert projects and musings to ignite your imagination.
  • Handpicked frontend development resources and current explorations, aimed at expanding your developer toolkit.
  • A monthly infusion of inspiration with my personal selection of quotes, books, and music.

Embrace the confluence of words and wonder, curated thoughtfully and sent straight to your inbox.

No fluff. Just the highest caliber of ideas.