-
-
Save wsvincent/8c9ba957c29d35faa65d15a89c88600c to your computer and use it in GitHub Desktop.
# users/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 = ['email', 'username',] | |
admin.site.register(CustomUser, CustomUserAdmin) |
# users/forms.py | |
from django import forms | |
from django.contrib.auth.forms import UserCreationForm, UserChangeForm | |
from .models import CustomUser | |
class CustomUserCreationForm(UserCreationForm): | |
class Meta(UserCreationForm): | |
model = CustomUser | |
fields = ('username', 'email') | |
class CustomUserChangeForm(UserChangeForm): | |
class Meta: | |
model = CustomUser | |
fields = ('username', 'email') |
# users/models.py | |
from django.contrib.auth.models import AbstractUser | |
from django.db import models | |
class CustomUser(AbstractUser): | |
# add additional fields in here |
1. Start a new Django project: `django-admin startproject new_project .` | |
2. Create a `users` app: `python manage.py startapp users` | |
3. Update `new_project/settings.py`, `users/models.py`, `users/forms.py`, `users/admin.py` | |
4. `python manage.py makemigrations users` | |
5. `python manage.py migrate` |
# new_project/settings.py | |
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
# Local | |
'users.apps.UsersConfig', # new | |
] | |
... | |
AUTH_USER_MODEL = 'users.CustomUser' # new |
After spending 4 hours I finally made it work. If you follow the Vincent's instructions and you admin page throws error please check your Python version. There was a bug with Python 3.7.0 where /admin/login get stuck in the look referring back to itself. https://code.djangoproject.com/ticket/31067
One thing that really hung me up was why my custom fields like 'photo' or 'relationship' wasn't showing in admin. After some searching I found something like the below to resolve the issue.
sources: https://stackoverflow.com/questions/15012235/using-django-auth-useradmin-for-a-custom-user-model, https://stackoverflow.com/questions/1437991/django-admin-fieldsets/1485005
class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm model = CustomUser list_display = ["username", "email", "photo"] # Fieldsets fieldsets = UserAdmin.fieldsets + ( ("Profile Image", { "fields": ("photo",) }), ("Relationships", { "fields": ("relationship",) }), ) add_fieldsets = ( (None, {"classes": ("wide",), "fields": ("username", "email", "photo")}), ) admin.site.register(CustomUser, CustomUserAdmin)
Nice, Can you please make an article related to custom user management:-
Send random password to email of user after create any user.
Each user can able to login on same panel with there provided credential.
Use JWT token instead of session
with templates and users app in navbar