22

I'm following a tutorial on making a custom User for authentication purposes. The tutorial used a certain property add_fieldsets within UserAdmin. What does this mean? I can't seem to find any documentation on this.

Here is the snippet:

class UserAdmin(UserAdmin):
"""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',)

Here is the tutorial I was following: How to use email as username for Django authentication (removing the username)

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Donovan Keating
  • 1,715
  • 3
  • 17
  • 27
  • The effect of this is that you start out with a limited set of form fields, and new fields "magically" appear on the form after you click "Save" (which in this case does the same as "Save and continue editing"). – djvg Jan 17 '19 at 20:19

5 Answers5

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.

djvg
  • 11,722
  • 5
  • 72
  • 103
LondonAppDev
  • 8,501
  • 8
  • 60
  • 87
  • 2
    The reason there is no documentation is probably because: "... `add_fieldsets` is **not a standard** `ModelAdmin` **attribute**. `UserAdmin` overrides `get_fieldsets` to use this attribute when creating a user." (from the auth/customizing/ docs linked above). It is just clever usage of the admin machinery. – djvg Jan 17 '19 at 19:55
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!!

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30
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',)
Al Mahdi
  • 635
  • 1
  • 9
  • 16
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

Ramon K.
  • 3,402
  • 3
  • 20
  • 29
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.
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129