admin.py
from django import forms
from django_sso_server import sso_admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.models import User
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 = ('username',)
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(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ('email', 'password', 'is_active', 'is_superuser', 'groups')
def clean_password(self):
return self.initial["password"]
class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('username', 'is_superuser',)
list_filter = ('username', 'is_superuser',)
fieldsets = (
(None, {'fields': ('username', 'password')}),
('Permissions', {'fields': ('is_superuser',)}),
('Groups', {'fields': ('groups',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2', 'groups')
}),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()
sso_admin.site.unregister(User)
sso_admin.site.register(User, MyUserAdmin)
- 部分字段说明
fieldsets
update的时候出现的field
image.png
add_fieldsets
add的时候出现的field
image.png
网友评论