Reputation: 3380
When editing user model through admin interface, here's what I see: And here's what I expect to see: The second one allows me to modify the user permissions, the first one does not.
The User model I use on the first screenshot inherits from AbstractUser
and is registered in the following way:
from django.contrib import admin
import accounts.models
admin.site.register(accounts.models.User)
In my settings:
DEBUG = True
AUTH_USER_MODEL = 'accounts.User'
What can be the problem? How do I get from the first screenshot to the second?
Upvotes: 1
Views: 1929
Reputation: 11
I got the same problem when using custom user following django docs https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#a-full-example on UserAdmin class I added on Permissions, fields: "groups", "user_permissions". I also added filter_horizontal = ("groups", "user_permissions"). Also I comment out the filter_horizontal at the bottom
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserChangeFormAdmin
add_form = UserCreationFormAdmin
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ["email", "first_name", "last_name", "is_active", "is_staff", "is_admin"]
list_filter = ["email", "is_active", "is_staff", "is_admin"]
fieldsets = [
(None, {
"fields": ["email", "password"]
}),
("Personal info", {
"fields": ["first_name", "last_name"]
}),
("Permissions", {
"fields": ["is_active", "is_staff", "is_admin", "groups", "user_permissions"]
}),
]
filter_horizontal = ("groups", "user_permissions")
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = [
(
None,
{
"classes": ["wide"],
"fields": ["first_name", "last_name", "email", "password1", "password2", "is_active", "is_staff"],
},
),
]
search_fields = ["email"]
ordering = ["email"]
# filter_horizontal = []
Upvotes: 0
Reputation: 53
The problem in my case is that, I was inheriting only from admin.UserAdmin, I had to create a UserAdmin class that inherits from admin.ModelAdmin and add the filter_horizontal to it, I'm adding the filter every time I modify the UserAdmin, it should look like:
class UserAdmin(UserAdmin):
model = User
filter_horizontal = ('groups', 'user_permissions')
add_form = UserCreationForm
form = UserChangeForm
list_display = ('email', 'is_staff', 'is_active',)
list_filter = ('email', 'is_staff', 'is_active',)
fieldsets = (
# ('Zone label',{'fields.....'})
(None, {'fields': ('email', 'username', 'password',)}),
('Permissions', {'fields': ('is_staff', 'is_active',)}),
('Personal', {'fields': ('about',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active')}
),
)
search_fields = ('email',)
ordering = ('date_joined',)
admin.site.register(User, UserAdmin)
admin.site.unregister(User)
class UserAdmin(admin.ModelAdmin):
list_display = ['username']
filter_horizontal = ("groups", "user_permissions")
admin.site.register(User, UserAdmin)
Upvotes: 0
Reputation: 3380
OK, the actual problem was that I inherited not from AbstractUser
but from AbstractBaseUser
and forgot about PermissionsMixin
(the mixin adds the apropriate fields). So I should've done something like this.
Upvotes: 1
Reputation: 81
Had the same issue but the solution is quite simple. In your admin.py file just add 'groups', 'user_permissions' to filter_horizontal = () i.e
filter_horizontal = ('groups', 'user_permissions')
that is basically it.
referenced from: https://djangobook.com/customizing-change-lists-forms/
Upvotes: 7