简体   繁体   中英

Custom User model fields (AbstractUser) not showing in django admin

I have extended User model for django, using AbstractUser method. The problem is, my custom fields do not show in django admin panel.

My models.py:

from django.contrib.auth.models import AbstractUser


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

My admin.py:

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

admin.site.register(User, UserAdmin)

Thanks

If all you want to do is add new fields to the standard edit form (not creation), there's a simpler solution than the one presented above.

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

from .models import User


class CustomUserAdmin(UserAdmin):
    fieldsets = (
        *UserAdmin.fieldsets,  # original form fieldsets, expanded
        (                      # new fieldset added on to the bottom
            'Custom Field Heading',  # group heading of your choice; set to None for a blank space instead of a header
            {
                'fields': (
                    'is_bot_flag',
                ),
            },
        ),
    )


admin.site.register(User, CustomUserAdmin)

This takes the base fieldsets, expands them, and adds the new one to the bottom of the form. You can also use the new CustomUserAdmin class to alter other properties of the model admin, like list_display , list_filter , or filter_horizontal . The same expand-append method applies.

You have to override UserAdmin as well, if you want to see your custom fields. There is an example here in the documentation.

You have to create the form for creating (and also changing) user data and override UserAdmin . Form for creating user would be:

class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = '__all__'

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user

You override UserAdmin with:

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

class UserAdmin(BaseUserAdmin):
    add_form = UserCreationForm
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'first_name', 'last_name', 'is_bot_flag', 'password1', 'password2')}
        ),
    )

and then you register:

admin.site.register(User, UserAdmin)

I pretty much copy/pasted this from documentation and deleted some code to make it shorter. Go to the documentation to see the full example, including example code for changing user data.

The quickest way to show your extra fields in the Django Admin panel for an AbstractUser model is to unpack the UserAdmin.fieldsets tuple to a list in your admin.py, then edit to insert your field/s in the relevant section and repack as a tuple (see below).
Add this code in admin.py of your Django app

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

fields = list(UserAdmin.fieldsets)
fields[0] = (None, {'fields': ('username', 'password', 'is_bot_flag')})
UserAdmin.fieldsets = tuple(fields)

admin.site.register(User, UserAdmin)

Note :
list(UserAdmin.fieldsets) gives the following list:

[  (None, {'fields': ('username', 'password')}), 
   ('Personal info', {'fields': ('first_name', 'last_name', 'email')}), 
   ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 
'user_permissions')}), 
   ('Important dates', {'fields': ('last_login', 'date_joined')})
]

These fields are by default in Django user models, and here we are modifying the first index of the list to add our custom fields.

Try this...


models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.
class CustomUser(AbstractUser):
    phone_number = models.CharField(max_length=12)

settings.py : Add below line of code in settings.py

AUTH_USER_MODEL = 'users.CustomUser'

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser


class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = CustomUser
        fields = '__all__'

admin.py

from django.contrib import admin
from .models import CustomUser
from .forms import CustomUserCreationForm
from django.contrib.auth.admin import UserAdmin


# Register your models here.
class CustomUserAdmin(UserAdmin):
    model = CustomUser
    add_form = CustomUserCreationForm
    fieldsets = (
        *UserAdmin.fieldsets,
        (
            'Other Personal info',
            {
                'fields': (
                    'phone_number',
                )
            }
        )
    )


admin.site.register(CustomUser, CustomUserAdmin)

After all are done then run below command in terminal

python manage.py makemigrations

python manage.py migrate

python manage.py runserver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM