Django show readonly field
Add readonly field in Django admin, django does not show readonly fields by default.
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
class BaseAdmin(admin.ModelAdmin):
readonly_fields = ('created_at', 'updated_at')
class Meta:
abstract = True
class BaseAdmin(admin.ModelAdmin):
readonly_fields = ('created_at', 'updated_at')
class Meta:
abstract = True
and then add this class in admin.py
class MyModelAdmin(BaseAdmin):
pass
class MyModelAdmin(BaseAdmin):
pass
register the model with this admin class.
admin.site.register(MyModel, MyModelAdmin)
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 🙏