I have created a custom user model which I am successfully using within my app.
我创建了一个自定义用户模型,我在我的应用程序中成功使用。
The problem is that within the Admin, on the user edit screen, I get a display of the present password hash, instead of the very useful interface for setting the password.
问题是在Admin中,在用户编辑屏幕上,我得到了当前密码哈希的显示,而不是用于设置密码的非常有用的界面。
I am using Django 1.5b1 on Python 2.7.
我在Python 2.7上使用Django 1.5b1。
How can I convince the Django admin to treat my user model the same way it treats the native User, for the sake of the admin user interface?
为了管理员用户界面,我如何说服Django管理员以对待本机用户的方式对待我的用户模型?
4 个解决方案
#1
9
Documentation suggest that you need to register the custom model with admin and also define few methods as well so that admin interface works with custom user model.
文档建议您需要使用admin注册自定义模型,并且还要定义一些方法,以便管理界面与自定义用户模型一起使用。
You may also have to define built-in forms for User.
您可能还必须为User定义内置表单。
From Custom users and django.contrib.admin
来自自定义用户和django.contrib.admin
You will also need to register your custom User model with the admin. If your custom User model extends AbstractUser, you can use Django's existing UserAdmin class. However, if your User model extends AbstractBaseUser, you'll need to define a custom ModelAdmin class.
您还需要向管理员注册自定义用户模型。如果您的自定义用户模型扩展了AbstractUser,则可以使用Django现有的UserAdmin类。但是,如果您的用户模型扩展了AbstractBaseUser,则需要定义自定义的ModelAdmin类。
#2
1
Just add this to your form:
只需将其添加到您的表单:
password = ReadOnlyPasswordHashField(
label= ("Password"),
help_text= ("Raw passwords are not stored, so there is no way to see "
"this user's password, but you can change the password "
"using <a href=\"password/\">this form</a>."))
Borrowed from https://*.com/a/15630360/780262
借用https://*.com/a/15630360/780262
#3
0
If you're extending AbstractBaseUser, you'll need two forms, a creation form, and an update form without the passwords, derived from ModelForm.
如果您正在扩展AbstractBaseUser,则需要两个表单,一个创建表单和一个没有密码的更新表单,这些表单派生自ModelForm。
Then in the Admin form, specify separate create and update forms, along with corresponding fieldsets:
然后在Admin窗体中,指定单独的创建和更新表单以及相应的字段集:
class MyUserAdmin(UserAdmin):
# The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (
(None, {'fields': ('email', 'first_name', 'last_name')}),
)
add_fieldsets = (
(None, {'fields': ('email', 'password', 'password2', 'first_name', 'last_name')}),
)
Passwords for new users will work. Do password changes in the pre-defined django form instead of in admin.
新用户的密码可以使用。在预定义的django表单而不是在admin中更改密码。
#4
0
At least in 1.10, I was able to do the following. Simpler than the above. I put it in admin.py:
至少在1.10,我能够做到以下几点。比上面简单。我把它放在admin.py中:
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from myapp.models import MyUser
class MyUserAdmin(UserAdmin):
model = MyUser
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('a_custom_field',)}),
)
admin.site.register(MyUser, MyUserAdmin)
#1
9
Documentation suggest that you need to register the custom model with admin and also define few methods as well so that admin interface works with custom user model.
文档建议您需要使用admin注册自定义模型,并且还要定义一些方法,以便管理界面与自定义用户模型一起使用。
You may also have to define built-in forms for User.
您可能还必须为User定义内置表单。
From Custom users and django.contrib.admin
来自自定义用户和django.contrib.admin
You will also need to register your custom User model with the admin. If your custom User model extends AbstractUser, you can use Django's existing UserAdmin class. However, if your User model extends AbstractBaseUser, you'll need to define a custom ModelAdmin class.
您还需要向管理员注册自定义用户模型。如果您的自定义用户模型扩展了AbstractUser,则可以使用Django现有的UserAdmin类。但是,如果您的用户模型扩展了AbstractBaseUser,则需要定义自定义的ModelAdmin类。
#2
1
Just add this to your form:
只需将其添加到您的表单:
password = ReadOnlyPasswordHashField(
label= ("Password"),
help_text= ("Raw passwords are not stored, so there is no way to see "
"this user's password, but you can change the password "
"using <a href=\"password/\">this form</a>."))
Borrowed from https://*.com/a/15630360/780262
借用https://*.com/a/15630360/780262
#3
0
If you're extending AbstractBaseUser, you'll need two forms, a creation form, and an update form without the passwords, derived from ModelForm.
如果您正在扩展AbstractBaseUser,则需要两个表单,一个创建表单和一个没有密码的更新表单,这些表单派生自ModelForm。
Then in the Admin form, specify separate create and update forms, along with corresponding fieldsets:
然后在Admin窗体中,指定单独的创建和更新表单以及相应的字段集:
class MyUserAdmin(UserAdmin):
# The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (
(None, {'fields': ('email', 'first_name', 'last_name')}),
)
add_fieldsets = (
(None, {'fields': ('email', 'password', 'password2', 'first_name', 'last_name')}),
)
Passwords for new users will work. Do password changes in the pre-defined django form instead of in admin.
新用户的密码可以使用。在预定义的django表单而不是在admin中更改密码。
#4
0
At least in 1.10, I was able to do the following. Simpler than the above. I put it in admin.py:
至少在1.10,我能够做到以下几点。比上面简单。我把它放在admin.py中:
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from myapp.models import MyUser
class MyUserAdmin(UserAdmin):
model = MyUser
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('a_custom_field',)}),
)
admin.site.register(MyUser, MyUserAdmin)