1

I have a custom user model:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    is_happy = models.BooleanField(default=False)

I'm referencing it within AUTH_USER_MODEL.

And I've generated an admin for it based on the UserAdmin

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

from .models import User


@admin.register(User)
class UserAdmin(DjangoUserAdmin):
    pass

But the is_happy field doesn't appear in the user admin page.

How/where do I tell this UserAdmin about additional fields that I'd like it to display?

Further details

  • Django v3.1.3
  • Python v3.8.5

2 Answers 2

5
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin

from .models import User


@admin.register(User)
class UserAdmin(DjangoUserAdmin):
    fieldsets = DjangoUserAdmin.fieldsets+ (
        (                      
            'Some heading', # you can also use None 
            {
                'fields': (
                    'is_happy',
                ),
            },
        ),
    )


admin.site.register(User, UserAdmin)
Sign up to request clarification or add additional context in comments.

Comments

1

There are two ways I think you can go about this.

But before that, I hope you have run the python manage.py makemigrations as well as python manage.py migrate so a s to reflects the changes in your database.

Then, you could simply make your admin.py file to be:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from .models import User

admin.site.register(User)

Or you could customize like this:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from .models import *

# Register your models here.

class UserAdmin(DjangoUserAdmin):
    fieldsets = (
        (None, {'fields': ('is_happy',)}),
        ('Permissions', {'fields': (
            'is_active',
            'is_staff',
            'is_superuser',
            'groups',
            'user_permissions',
        )}),
    )
    add_fieldsets = (
        (
            None,
            {
                'classes': ('wide',),
                'fields': ('username', 'password1', 'password2')
            }
        ),
    )

    list_display = ('email', 'is_happy', 'is_staff', 'last_login')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')

admin.site.register(User, UserAdmin)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.