显示用于将密码添加到自定义用户模型的字段-Django

发布于 2025-02-10 21:58:11 字数 683 浏览 1 评论 0原文

我有一个自定义用户模型。我希望管理员从管理面板创建新帐户。注册课无济于事。

admin.site.register(CustomUser)

*这具有避免使用密码重复输入密码的可能性

,因此我尝试了此解决方案:

from django.contrib.auth.admin import UserAdmin

admin.site.register(CustomUser, UserAdmin)

*在上述选项中,我可以选择两次输入密码(我创建用户),但是我的自定义中的其他字段用户模型正在消失。

class CustomUser(AbstractUser):
    name = models.CharField(max_length=50, blank=True, null=True)
    surname = models.CharField(max_length=50, blank=True, null=True)
    account_type = models.ForeignKey(Locationd, on_delete=models.CASCADE, blank=True, null=True

如何在管理面板中添加创建帐户的功能,提供可以发送给新用户并从Custome用户类中编辑所有字段的密码。

I have a custom user model. I want the admin to create new accounts from the admin panel. Registering a class doesn't help.

admin.site.register(CustomUser)

*This has the effect of avoiding the possibility of double-entering the password

So I try this solution:

from django.contrib.auth.admin import UserAdmin

admin.site.register(CustomUser, UserAdmin)

*In the above option, I have the option to enter the password twice (whean i create user), but the other fields from my custom user model are disappearing.

class CustomUser(AbstractUser):
    name = models.CharField(max_length=50, blank=True, null=True)
    surname = models.CharField(max_length=50, blank=True, null=True)
    account_type = models.ForeignKey(Locationd, on_delete=models.CASCADE, blank=True, null=True

How to add the ability to create accounts in the admin panel, giving a password that can be sent to the new user and editing all fields from the custome user class.

)

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

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

发布评论

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

评论(2

倒带 2025-02-17 21:58:12

您可以从旧项目中使用自己的django表格

from .models import User
from .forms import SignUpForm
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin

class ManagingUsers(UserAdmin):

    add_form = SignUpForm

    list_display = ['__str__']
    search_fields = ['email']
    readonly_fields = ['date_joined', 'last_login']
    ordering = ('id',)

    filter_horizontal = []
    list_filter = []


    add_fieldsets = (

        ('Add a new user', {
            'classes':('wide',),
            'fields': ('first_name', 'last_name' ,'email', 'birthday', 'password1', 'password2', 'avatar', 'cover_pic', 'validated', 'is_admin', 'is_staff', 'is_active', 'is_superuser'),
            }),
        )
    
    fieldsets = (

        (None, {
            'fields': ('first_name', 'last_name', 'bio' ,'email', 'birthday', 'avatar', 'cover_pic', 'is_admin',  'is_staff', 'is_active', 'is_superuser'),
            }),

        )
admin.site.register(User, ManagingUsers)

此代码,因此将字段名称更改为您想要的任何内容

You can use your own Django form

from .models import User
from .forms import SignUpForm
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin

class ManagingUsers(UserAdmin):

    add_form = SignUpForm

    list_display = ['__str__']
    search_fields = ['email']
    readonly_fields = ['date_joined', 'last_login']
    ordering = ('id',)

    filter_horizontal = []
    list_filter = []


    add_fieldsets = (

        ('Add a new user', {
            'classes':('wide',),
            'fields': ('first_name', 'last_name' ,'email', 'birthday', 'password1', 'password2', 'avatar', 'cover_pic', 'validated', 'is_admin', 'is_staff', 'is_active', 'is_superuser'),
            }),
        )
    
    fieldsets = (

        (None, {
            'fields': ('first_name', 'last_name', 'bio' ,'email', 'birthday', 'avatar', 'cover_pic', 'is_admin',  'is_staff', 'is_active', 'is_superuser'),
            }),

        )
admin.site.register(User, ManagingUsers)

This code from an old project so change the fields names to whatever you want

前事休说 2025-02-17 21:58:12

您需要取消注册用户模型,然后添加自定义用户

from django.contrib.auth.models import User

admin.site.unregister(User)
admin.site.register(CustomUser, UserAdmin or YourCustomAdminSettings)

You need to unregister the user model then add your custom user

from django.contrib.auth.models import User

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