Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django UserAdmin's add_fieldsets?

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)

like image 586
Donovan Keating Avatar asked May 20 '18 15:05

Donovan Keating


People also ask

How do I authenticate an email in Django?

Email authentication for Django 3.x For using email/username and password for authentication instead of the default username and password authentication, we need to override two methods of ModelBackend class: authenticate() and get_user():

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


1 Answers

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.

like image 114
LondonAppDev Avatar answered Sep 17 '22 15:09

LondonAppDev