Adding new fields in ModelAdmin with fieldsets to edit user, and add_fieldsets whan creating a new user 1

Adding new fields in ModelAdmin with fieldsets to edit user, and add_fieldsets whan creating a new user

from django.contrib.auth.admin import UserAdmin

class CustomUserAdmin(UserAdmin):
    ...
    fieldsets = UserAdmin.fieldsets + (
        (None, {'fields': ('custom_field',)}),
    )
    add_fieldsets = UserAdmin.add_fieldsets + (
        (None, {'fields': ('custom_field',)}),
    )

Here is what the above code is Doing:
1. We’re importing the UserAdmin class from the django.contrib.auth.admin module.
2. We’re creating a new class called CustomUserAdmin that inherits from UserAdmin.
3. We’re adding a new fieldset to the UserAdmin fieldsets tuple.
4. We’re adding a new fieldset to the UserAdmin add_fieldsets tuple.

Similar Posts