Skip to content

Instantly share code, notes, and snippets.

@leisurelicht
Last active August 20, 2024 00:44
Show Gist options
  • Save leisurelicht/ad469c23e90c856e69075585ed40f1db to your computer and use it in GitHub Desktop.
Save leisurelicht/ad469c23e90c856e69075585ed40f1db to your computer and use it in GitHub Desktop.
Make all fields readonly for Django Admin
class OpsIPInfoAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# make all fields readonly
readonly_fields = list(
set([field.name for field in self.opts.local_fields] +
[field.name for field in self.opts.local_many_to_many])))
if 'is_submitted' in readonly_fields:
readonly_fields.remove('is_submitted')
return readonly_fields
@hugobelem
Copy link

Hey, beginner question:
How do I find the standard field keys for attributes like 'Superuser status', 'Staff status', 'Active', etc.?
I want to make a few fields read-only, but I couldn’t find the correct names.

class CustomUserAdmin(admin.ModelAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm model = CustomUser list_display = ['first_name', 'username', 'email', 'phone'] search_fields = ['first_name__startswith'] readonly_fields = ['date_joined', 'last_login']

In this code, I set 'date_joined' and 'last_login' as read-only, but how can I set 'Superuser status' and 'Staff status' as read-only, for example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment