Reputation: 541
I created a cutom user model for my project. And the extra fileds I created is not showing in the Admin page.
I just see the standard fields and none of the extra made. I can't fins anything online about what i'm missing.
My model for customuser:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
email = models.EmailField(blank=False, null=False)
firstname = models.CharField(max_length=25, blank=True, null=True)
surname = models.CharField(max_length=45, blank=True, null=True)
mobilephone = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.email
My admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ["surname", "firstname", "email", "mobilephone"]
admin.site.register(CustomUser, CustomUserAdmin)
Also, the field 'First name' and 'Last name' is not the same field as my custom user model.
Upvotes: 1
Views: 796
Reputation: 2045
Since you created a custom User Admin derived from Django's UserAdmin
You need to modify the fieldsets
attribute in it.
This is how it is defined in UserAdmin
class.
fieldsets = (
(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')}),
)
In order to add your field under Personal info
fieldset, like the following
fieldsets = (
...
(_('Personal info'), {'fields': ('firstname', 'surname', 'email', 'mobilephone')}),
...
)
Upvotes: 2
Reputation: 5854
try this
class CustomUserAdmin(UserAdmin):
# ----
fieldsets = (
(None, {'fields': (
# in build
('first_name',),
('last_name',),
('username',),
# custom
('surname'),
)
)
More https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets
Upvotes: 0