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
-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.
- [Django]-Django Rest framework: passing request object around makes it lose POST data in newer version
- [Django]-Why is this logging configuration not printing to stdout?
- [Django]-How to generate unique 8 length number, for account ID for example (Python,Django)
- [Django]-How to use django default group permission in django-graphene?