3

How can I hide fields in admin User edit? Mainly I want to hide permissions and groups selecting in some exceptions, but exclude variable doesn't work :/

robos85
  • 2,484
  • 5
  • 32
  • 36

2 Answers2

10

I may be late to answer this question but any ways, here goes. John is right in concept but I just wanted to do this because I know django admin is truly flexible.

Any way's the way you hide fields in User model form is:

1. exclude attribute of the ModelAdmin class can be used to hide the fields.

2: The should allow blank in model.

3: default attribute on model field is an advantage or you might get unexpected errors.

The problems I had was that I used to get a validation error. I looked at the trace back and found out that the error was because of UserAdmin's fieldsets grouping, the default permission field set has user_permission override this in your sub-calassed model admin.

Use the exclude attribute in get_form where you can access request variable and you can set it dynamical depending on the user's permission or group.

Code:

admin.py:

class MyUserAdmin(UserAdmin): 

     list_display = ("username","first_name", "last_name", "email","is_active","is_staff","last_login","date_joined")

     ## Static overriding 
     fieldsets = (
         (None, {'fields': ('username', 'password')}),
         (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
         (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                    'groups')}),
     (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
     )


     def get_form(self, request, obj=None, **kwargs):
         self.exclude = ("user_permissions")
         ## Dynamically overriding
         self.fieldsets[2][1]["fields"] = ('is_active', 'is_staff','is_superuser','groups')
         form = super(MyUserAdmin,self).get_form(request, obj, **kwargs)
         return form
eandersson
  • 25,781
  • 8
  • 89
  • 110
Pannu
  • 2,547
  • 2
  • 23
  • 29
  • 1
    I know this is an old question but, is this thread safe? `self.exclude` is a class attribute so you could obtain unwanted results. I think the way to go is by overriding the new `get_fields`, `get_fieldsets`, etc. methods. Am I right? – argaen Mar 11 '15 at 13:37
  • 1
    @argaen you are correct; assigning to `self.exclude` (or any other class attribute like `fields`, `readonly_fields`, etc) after initialization is not safe, because there is only a single instance of each `ModelAdmin` created per process. Dynamically changing which fields are displayed *must* be done with `get_fields()` and similar methods. – Myk Willis Jan 24 '16 at 14:42
-5

The django admin is not designed for very fine grained control so their are no automated variables designed to allow this type of control.

If you need this type of control you're going to have to go it your own. You'll need to override the default admin templates. You'll probably want to use the permissions system to track what users are allowed to do.

Keep in mind the level of customization you're making. At some point working to far outside the intended purpose and limitations of the admin app will be more work than simply rolling your own more fine grained CRUD system.

John
  • 5,166
  • 27
  • 31
  • 8
    You can achieve very high levels of fine grain control as long as you actually read the manual. – Shayne Feb 23 '15 at 05:42