Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin - make all fields readonly

I'm trying to make all fields readonly without listing them explicitly.

Something like:

class CustomAdmin(admin.ModelAdmin):     def get_readonly_fields(self, request, obj=None):         if request.user.is_superuser:             return self.readonly_fields          return self.fields 

The problem is CustomAdmin.fields is not set at this point.

Any ideas?

like image 810
yprez Avatar asked Dec 11 '12 09:12

yprez


1 Answers

Since django 2.1, you can prevent editing, while allowing viewing, by returning False from the ModelAdmin's has_change_permission method, like this:

class CustomAdmin(admin.ModelAdmin):     def has_change_permission(self, request, obj=None):         return False 

(This will not work before django 2.1, as it will also deny permission to any user trying only to view.)

like image 95
ცოტნე შარვაძე Avatar answered Sep 21 '22 12:09

ცოტნე შარვაძე