def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
# 注册用户
class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreateForm
list_display = ('name', 'created_at', 'email', 'is_delete', 'is_admin')
search_fields = ('name', 'email')
list_filter = ('is_admin',)
readonly_fields = ('created_at', 'updated_at')
fieldsets = (
(None, {'fields': ('name', 'email', 'password', 'avatar',)}),
('Personal info', {'fields': ('created_at', 'updated_at')}),
(
'Open token info',
{
'fields': ('access_token', 'refresh_token', 'expires_in')
}
),
('Permissions', {'fields': ('is_delete', 'is_admin', 'is_active')}),
('Important dates', {'fields': ('last_login',)}),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('name', 'email', 'password1', 'password2'),
}
),
)
ordering = ('created_at',)
filter_horizontal = ()
admin.site.register(User, MyUserAdmin)
step-3 修改settings.py中相应配置
添加 AUTH_USER_MODEL = 'myauth.User'
install_app不要忘记加上myauth模块
grep django的源码可以看到,很多地方直接使用了配置AUTH_USER_MODEL
user = models.ForeignKey(settings.AUTH_USER_MODEL)
def get_user_model():
"""
Returns the User model that is active in this project.
"""
from django.db.models import get_model
try:
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
user_model = get_model(app_label, model_name)
if user_model is None:
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
return user_model
ps: django admin更改主题,django admin的主题实在是太朴素了
https://riccardo.forina.me/bootstrap-your-django-admin-in-3-minutes/