A model won't be displayed on the admin panel, until its registered.
To register a model for the admin page, you can open the authentication/admin.py
and add the following code :
from django.contrib import admin
from .models import *
admin.site.register(User)
After adding this code, if you visit the admin page you will be able to view the Users model. On selecting the existing User, you would be able to view its details. For now it is not nice, as fields are not in any particular order. You can correct that by giving them an order and even grouping them into separate sections so that it will be easy to edit. This can be achieved by changing the code as follows :
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name', 'profile_photo', 'mobile_number')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'role','groups', 'user_permissions')}),
('Important Dates', {'fields': ('last_login', 'date_joined')}),
)
Here, the field sets are grouped separately for login fields, personal information, permissions and dates. This makes it more easy to view and edit the details.
Coming to the groups and user permissions fields, a UI like this would be present.
A multi select field like this makes it difficult to edit or select the permissions sometimes. A better approach is to give these fields inside filter_horizontal
, this will allow it to be edited more easily with a better UI.
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name', 'profile_photo', 'mobile_number')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'role','groups', 'user_permissions')}),
('Important Dates', {'fields': ('last_login', 'date_joined')}),
)
filter_horizontal = ('groups', 'user_permissions')
Adding the last line, would change the UI like this. This allows it take selected options into another box and it can be moved along both easily. This is a more User friendly while operating with multiselect fields.