make django model field read only or disable in admin while saving the object first time
There is no need to override get_readonly_fields
. Simplest solution would be:
class ItemAdmin(admin.ModelAdmin):
exclude=("headline ",)
readonly_fields=('headline', )
When using readonly_fields
you can't override get_readonly_fields
, because default implementation reads readonly_fields variable. So overriding it only if you have to have some logic on deciding which field should be read-only at time.
If you want to make the field read-only during creation you should do it the other way round:
def get_readonly_fields(self, request, obj=None):
if obj is None:
return ['headline']
return []