[Django]-What is "add_fieldsets" for in UserAdmin in Django?

22👍

The add_fieldsets class variable is used to define the fields that will be displayed on the create user page.

Unfortunately it’s not well documented, and the closest thing I found to documentation was a comment in the code example on https://docs.djangoproject.com/en/2.1/topics/auth/customizing/ (search page for add_fieldsets).

The classes key sets any custom CSS classes we want to apply to the form section.

The fields key sets the fields you wish to display in your form.

In your example, the create page will allow you to set an email, password1 and password2.

3👍

The add_fieldsets field works similar to fieldsets field and allows to control the layout of the admin pages but specifically the create object page whereas fieldsets field is used to control change object page.

And using add_fieldsets field you can decide which all fields should be displayed on the create object page, add description ,etc.

In your example it will create a section with no name (as set to None), set a specific width to all the fields by using classes key set and display fields email,password1 and password2
by using key set fields

I hope I cleared your doubt!!

1👍

use the BaseUserAdmin class, it enables some functionalities like add_form and add_fieldsets

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

class UserAdmin(BaseUserAdmin):
    """Define admin model for custom User model with no email field."""

    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('first_name', 'last_name')}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        ('Important dates', {'fields': ('last_login', 'date_joined')}),
        ('Contact info', {'fields': ('contact_no',)}),)

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),}),)

    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)

0👍

From the Django documentation (see the Note):

If you are using a custom ModelAdmin which is a subclass of django.contrib.auth.admin.UserAdmin, then you need to add your custom fields to fieldsets (for fields to be used in editing users) and to add_fieldsets (for fields to be used when creating a user).

So it’s basically the same as fieldsets, but for creating users and only needed when you’re extending UserAdmin. The only difference between fieldsets and add_fieldsets is that you have to treat password fields a little different ("password1" and "password2", instead of "password").

The documentation offers a full example of implementing a custom user here: https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#custom-users-admin-full-example

0👍

add_fieldsets:

  • can set fields to display them in Add User page.
  • is the special option which only UserAdmin class has so ModelAdmin class doesn’t have it. *UserAdmin class extends ModelAdmin class.

Leave a comment