在 django 中扩展用户管理表单

发布于 2024-12-14 06:41:08 字数 684 浏览 4 评论 0 原文

我正在尝试更改 Django 中的用户管理员。在我的项目中,需要电子邮件地址、名字和姓氏。我更改了我的用户管理,如下所示:

class UserForm(forms.ModelForm):
    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

class UserAdmin(admin.ModelAdmin):
    form = UserForm
    list_display = ('first_name','last_name','email','is_active')

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

问题是每当我用密码保存用户时,它都会显示为没有哈希值。我想问题是,我需要用新表单对密码字段进行哈希处理。但旧的形式可以做到这一点,那么有没有办法可以扩展旧的形式?

I'm trying to change user admin in Django. In my project the email address, first name and last name is required. I changed my user admin as following :

class UserForm(forms.ModelForm):
    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

class UserAdmin(admin.ModelAdmin):
    form = UserForm
    list_display = ('first_name','last_name','email','is_active')

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

The problem is whenever I save a user with a password, it's displayed as without hashing. I guess the problem is, I need to hash the password field with my new form. But the old form does it, so is there a way that I can extend the oldform ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-12-21 06:41:08

您可以在 UserChangeForm 进行子类化>django.contrib.auth.forms,并自定义其行为,而不是子类化 forms.ModelForm

from django.contrib.auth.forms import UserChangeForm

class MyUserChangeForm(UserChangeForm):
    def __init__(self, *args, **kwargs):
        super(MyUserChangeForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

class UserAdmin(admin.ModelAdmin):
    form = MyUserChangeForm

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

上面将使用用户密码的默认行为,即显示密码哈希,并链接到密码更改表单。如果你想修改它,我会查看 SetPasswordForm,看看如何在 Django 管理中设置密码。

You can subclass the existing UserChangeForm in django.contrib.auth.forms, and customise its behaviour, rather than subclassing forms.ModelForm.

from django.contrib.auth.forms import UserChangeForm

class MyUserChangeForm(UserChangeForm):
    def __init__(self, *args, **kwargs):
        super(MyUserChangeForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

class UserAdmin(admin.ModelAdmin):
    form = MyUserChangeForm

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

The above will use the default behaviour for the user password, which is to display the password hash, and link to the password change form. If you want to modify that, I would look at SetPasswordForm, to see how the password is set in the Django admin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文