Home > OS >  Customizing UserChangeForm in django
Customizing UserChangeForm in django

Time:11-29

I want to delete the 'groups' field from the build-in UserChangeForm and add my own "Role' field. How to handle that? I created my class which inherits from UserChangeForm and went to somehow delete but no change.

class CustomUserChangeForm(UserChangeForm):
    def __init__(self, *args, **kwargs):
        super(CustomUserChangeForm, self).__init__(*args, **kwargs)
        print(self.fields)

    class Meta(UserChangeForm.Meta):
        fields = ()
        exclude = ('groups',)


class CustomUserAdmin(UserAdmin):
    change_form = CustomUserChangeForm
    def __init__(self, *args, **kwargs):
        super(UserAdmin, self).__init__(*args, **kwargs)
        UserAdmin.list_display = ('username', 'is_active', 'email',
                                  'date_joined', 'user_role', 'is_staff', 'is_superuser')


class RoleAdmin(admin.ModelAdmin):
    fields = ('name', 'role_permissions')


admin.site.unregister(Group)
admin.site.unregister(User)
admin.site.register(Role, RoleAdmin)
admin.site.register(User, CustomUserAdmin)


CodePudding user response:

You can inherit the Meta of the UserChangeForm and rewrite the exclude field to exclude the groups:

class CustomUserChangeForm(UserChangeForm):
    
    class Meta(UserChangeForm.Meta):
        fields = None
        exclude = ('groups',)

For the admin site you can work with:

from django.contrib.auth import get_user_model

class CustomUserAdmin(UserAdmin):
    form = CustomUserChangeForm
    list_display = ('username', 'is_active', 'email', 'date_joined',
                    'user_role', 'is_staff', 'is_superuser')

User = get_user_model()

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

CodePudding user response:

I deleted UserChangeForm, and I define fieldsets in CustomUseAdmin like this: https://github.com/django/django/blob/main/django/contrib/auth/admin.py#L45

And works correctly, but I can't add my 'Role' model here

class CustomUserAdmin(UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Permissions'), {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'user_role', 'user_permissions'),
        }),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    
    def __init__(self, *args, **kwargs):
        super(UserAdmin, self).__init__(*args, **kwargs)
        UserAdmin.list_display = ('username', 'is_active', 'email',
                                  'date_joined', 'user_role', 'is_staff', 'is_superuser')

Unknown field(s) (user_role) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.

class Role(models.Model):
    name = models.CharField(max_length=100, unique=True)
    user = models.OneToOneField(User, related_name="user_role", on_delete=models.CASCADE)
    role_permissions = models.ManyToManyField(Permission, related_name="role_permissions")
    
    class Meta:
        app_label = 'auth'
  • Related