我之前已经问过类似的问题,但我无法解决这个问题。
我已覆盖用户管理模板,因此我只能显示用户的特定字段。这很有效。
但我无法编辑用户密码,因为它显示为哈希值。我想像以前一样发布一个链接。 我看了解解决方案,但他们没有工作。我的代码如下:
class UserAdmin(admin.ModelAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 'is_superuser')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
list_display = ('username', 'email', 'first_name', 'last_name')
list_filter = ('is_active', 'groups')
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>."))
admin.site.unregister(User)
admin.site.register(User,UserAdmin)
所有这一切都运行良好,但密码显示为一个简单的字段,文字没有出现......
谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
这就是解决方案:
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>."))
this form
会链接到它。问题是你错过了这个。它应该在UserChangeForm下。例如(取决于您正在使用的用户表):
class AdminUserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField(
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>."
)
class Meta:
model = User
fields = (
'email', 'first_name', 'last_name',
)
def clean_password(self):
...
return self.initial["password"]
class UserAdmin(BaseUserAdmin):
...
fieldsets = (
('Login', {
'fields': [
'email',
'password',
]
}),
...
)
...
form = AdminUserChangeForm
add_form = AdminUserCreationForm