Django has no read-only permission by default and can only be implemented by itself. I checked a lot of information and finally got it done.
Modify Admin. py
From Django. contrib. admin. util import flatten_fieldsets # introduce class
Class gconfadmin (Admin. modeladmin ):
List_display = ('mailserver', 'mailsender', 'mail _ on_off ', 'sms _ on_off ')
# Search fields
Search_fields = ('mailserver', 'mailsender', 'mail _ on_off ', 'sms _ on_off ')
Def get_readonly_fields (self, request, OBJ = none ):
If not request. User. is_superuser and request. User. Username = 'readonly ':
Return [F. Name for F in self. model. _ meta. Fields]
Return self. readonly_fields
Add the red part to the admin class
The root user logs on to the Django interface, creates a readonly user, and grants the change permission (the user must have the change permission)
The disadvantage of this method is that each mymodeladmin class needs to be addedGet_readonly_fieldsFunction
Optimization Method:
Create custom_model_admin.py in the project app
From Django. contrib import Admin
From Django. contrib. admin. util import flatten_fieldsets
Class custommodeladmin (Admin. modeladmin ):
Def get_readonly_fields (self, request, OBJ = none ):
If not request. User. is_superuser and request. User. Username = 'readonly ':
Return [F. Name for F in self. model. _ meta. Fields]
Return self. readonly_fields
Reference this class in Admin. py
From MyApp. custom_model_admin import custommodeladmin
Replace Admin. modeladmin with custommodeladmin.
Add a readonly user to Django Admin