如何使用抽象类和密码哈希等所有工作函数在 Django 默认用户模型中添加额外字段[重复]

问题描述 投票:0回答:1

如何在默认用户模型中添加额外字段并将通过用户名登录更改为电子邮件。使用抽象类的问题是,当我使用抽象类时,默认函数,如密码哈希、所有验证等,它不起作用

django django-models django-users
1个回答
0
投票

要向默认用户模型添加额外的字段,您需要使用 AbstractUser。

django 文档上有一个 示例,它准确地展示了如何使用电子邮件而不是用户名登录以及添加自定义字段。

唯一没有明确提到的是,您必须创建一个应用程序,例如通过运行创建一个名为“customauth”的应用程序

python manage.py startapp customauth

然后您可以按照文件 customauth/models.py 和 customauth/admin.py 中的教程进行操作

您还需要在主应用程序中修改settings.py

INSTALLED_APPS = [
    #other installed apps
    'customauth', #add customauth to the installed apps
]

并且您需要在首次运行后迁移更改

python manage.py makemigrations

然后跑步

python manage.py migrate
© www.soinside.com 2019 - 2024. All rights reserved.