gpt4 book ai didi

django - 从 AbstractUser 继承的模型不会散列密码字段

转载 作者:行者123 更新时间:2023-12-02 18:09:32 32 4
gpt4 key购买 nike

我有一个继承 AbstractUser 的模型,如下所示:

class Driver(AbstractUser):
dni = models.CharField(max_length=8,validators=[validate_dni],unique=True)
license = models.CharField(max_length=9,unique=True)
birthday = models.DateField()
sex = models.CharField(max_length=1, choices=SEX_CHOICES)
creation_date = models.DateField(auto_now = True)

据此:https://docs.djangoproject.com/en/dev/topics/auth/customizing/

If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

但是,在我的管理 View 中,密码字段是一个简单的文本输入,并且密码保存为原始文本。我可以尝试使用 AbstractBaseUser 但首先我需要澄清这个问题。我从 Django 开始,所以我是个新手。

谢谢。

最佳答案

您不必实际定义自己的函数。您只需使用 django.contrib.auth.admin 中的 UserAdmin 类进行注册,即可开箱即用。

明确地,在您的 admin.py 文件中确保您具有以下内容:

from django.contrib.auth.admin import UserAdmin
admin.site.register(CustomUserModel, UserAdmin)

如果您的模型上有其他自定义字段,上述注册方式将使它们不会显示在管理中。在这种情况下,您可以通过使自定义 Admin 类继承 UserAdmin 类来使其工作,如下所示:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

@admin.register(CustomUserModel)
class CustomUserModelAdmin(UserAdmin):
...

关于django - 从 AbstractUser 继承的模型不会散列密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377544/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com