From 33af7f13d183912c18f951f61d8a287f9668ce5b Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 22 Jul 2020 00:52:02 +0200 Subject: [PATCH] Add admin stuff for custom users --- users/admin.py | 28 ++++++++++++++++++++++++++++ users/forms.py | 15 +++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 users/admin.py create mode 100644 users/forms.py diff --git a/users/admin.py b/users/admin.py new file mode 100644 index 0000000..2980848 --- /dev/null +++ b/users/admin.py @@ -0,0 +1,28 @@ +from django.contrib import admin +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 = ('username', 'is_staff', 'is_superuser') + list_filter = ('username', 'is_staff') + fieldsets = ( + (None, {'fields': ('username', 'password')}), + ('Permissions', {'fields': ('is_staff', 'is_superuser')}), + ) + add_fieldsets = ( + (None, { + 'classes': ('wide',), + 'fields': ('username', 'password1', 'password2', 'is_staff')} + ), + ) + search_fields = ('username',) + ordering = ('username',) + + +admin.site.register(CustomUser, CustomUserAdmin) diff --git a/users/forms.py b/users/forms.py new file mode 100644 index 0000000..d08ecbd --- /dev/null +++ b/users/forms.py @@ -0,0 +1,15 @@ +from django.contrib.auth.forms import UserCreationForm, UserChangeForm + +from .models import CustomUser + + +class CustomUserCreationForm(UserCreationForm): + class Meta(UserCreationForm): + model = CustomUser + fields = ('username',) + + +class CustomUserChangeForm(UserChangeForm): + class Meta: + model = CustomUser + fields = ('username',)