Field Readonly After Save
get_readonly_fields
If you want to make a field editable before save
and readonly after save
. You can override get_readonly_fields
in admin.py
to realize it
Definition
get_readonly_fields(self, request, obj=None)
Usage
Make Appointed Fields Readonly
from django.contrib import admin class MyModelAdmin(admin.ModelAdmin): def get_readonly_fields(self, request, obj=None): if obj: # editing an existing object return self.readonly_fields + ('field1', 'field2') return self.readonly_fields
field1
andfield2
will beeditable before save
andreadonly after save
Make All Fields Readonly
from django.contrib import admin class MyModelAdmin(admin.ModelAdmin): def get_readonly_fields(self, request, obj=None): if obj: # editing an existing object return self.readonly_fields + tuple(obj._meta.get_all_field_names()) # return self.readonly_fields + tuple(f.name for f in self.model._meta.fields) # return self.readonly_fields + tuple(f.name for f in obj._meta.fields) return self.readonly_fields
References
[1] yprez@StackOverflow, Django admin - make all fields readonly