在Django中扩展用户模型可公开保存密码 [英] Extending user models in Django saves password publicly
问题描述
新创建的用户的密码在管理控制台模型上公开显示.为什么会这样,以及我如何正确执行呢?
The password for newly created users is shown publicly on the admin console models. Why is that and how I do it correctly?
此外,我实际上无法使用在 Accounts_app 中创建的任何新用户登录.我只能使用 python manage.py createsuperuser
登录我是在项目的早期创建的.
Furthermore, I am not actually able to login with any of the new users created in the Accounts_app. I am able to login only with the python manage.py createsuperuser
I created at the early point in the project.
这是 models.py
from django.contrib.auth.models import AbstractUser
class ProjectUser(AbstractUser):
def __str__(self):
return self.username
这是 settings.py
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.Argon2PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
]
AUTH_USER_MODEL = 'accounts_app.ProjectUser'
这是我的管理员视图
要创建用户,请在应用程序管理员视图中单击添加用户".
To create the user, I click "Add User" in the app admin view.
这是 apps.py 文件
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = 'accounts_app'
这是 admin.py 文件
from django.contrib import admin
from accounts_app.models import ProjectUser
# Register your models here.
admin.site.register(ProjectUser)
推荐答案
尽管将ProjectUser设置为AUTH_USER_MODEL,但您在admin中将其注册为标准模型,而不是用户模型.您需要使用用户admin,如
Although you set ProjectUser to be the AUTH_USER_MODEL, you registered it in the admin as a standard model, not the user one. You need to use the user admin, as shown in the docs, since this takes care of hashing the password:
from django.contrib.auth.admin import UserAdmin
admin.site.register(ProjectUser, UserAdmin)
您需要先删除并重新创建通过管理员生成的用户,然后再进行更改.
You'll need to delete and recreate the users you generated via the admin before changing this.
这篇关于在Django中扩展用户模型可公开保存密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!