Add a readonly user to Django Admin

Source: Internet
Author: User

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.

  1. 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

  2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.