简体   繁体   English

使用管理员身份注册自定义用户 model

[英]Register custom user model with admin auth

In a Django project, I have a custom user model that adds one extra field:在 Django 项目中,我有一个自定义用户 model 添加了一个额外的字段:

class User(AbstractUser):

    company = models.ForeignKey(Company, null=True, blank=True)

This model is defined in my app, say MyApp.models .这个 model 在我的应用程序中定义,比如MyApp.models

How can I get the new User model to show up under "Authentication and Authorization" as the original django.contrib.auth model?如何让新User model 显示在“身份验证和授权”下,作为原始django.contrib.auth model?

在此处输入图像描述

class User(AbstractUser):

    class Meta:
    app_label = 'auth'

This can solve your problem but may cause some errors when you migrate your app.这可以解决您的问题,但可能会在您迁移应用程序时导致一些错误。 The other hack is define get_app_list in your AdminSite .其他黑客的定义get_app_listAdminSite

I think what you're looking for is replacing the django user model.我认为您正在寻找的是替换 django 用户模型。 To do this, see the answer on this post: Extending the User model with custom fields in Django .为此,请参阅这篇文章的答案: 在 Django 中使用自定义字段扩展用户模型 I would suggest going the extending route, but that would mean both parent and child model would have to be registered.我建议走扩展路线,但这意味着必须注册父模型和子模型。

If you really want one single model, just set the AUTH_USER_MODEL setting to your model.如果您真的想要一个模型,只需将AUTH_USER_MODEL设置设置为您的模型。 This essentially replaces the default model.这基本上取代了默认模型。

In your settings.py:在您的 settings.py 中:

AUTH_USER_MODEL = "appname.UserModelName"

For more info on replacing the user model, see https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model .有关替换用户模型的更多信息,请参阅https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model

try this:尝试这个:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _

class AdminExtra(UserAdmin):
    add_form_template = "admin/auth/user/add_form.html"
    change_user_password_template = None
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("first_name", "last_name", "email", "nick_name")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
    )
    add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": ("username", "password1", "password2"),
            },
        ),
    )


admin.site.register(User, AdminExtra)

In admin.py use在 admin.py 中使用

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

UserAdmin.list_display = ('email', 'first_name', 'last_name', 'is_active', 'date_joined', 'is_staff')

admin.site.register(User, UserAdmin)

Might want to try this:可能想试试这个:

https://libraries.io/pypi/django-modeladmin-reorder https://libraries.io/pypi/django-modeladmin-reorder

Custom ordering for the apps and models in the admin app.管理应用程序中应用程序和模型的自定义排序。 You can also rename, cross link or exclude models from the app list.您还可以从应用程序列表中重命名、交叉链接或排除模型。

How can I get the new User model to show up under "Authentication and Authorization"如何让新的用户模型显示在“身份验证和授权”下

I had the same concern using Django 3.0, this is how I solved it :我使用 Django 3.0 也有同样的问题,这就是我解决它的方法:

class CustomUser(AbstractUser):
    
     class Meta:
         db_table = 'auth_user'
         app_label = 'auth'

In settings :在设置中:

AUTH_USER_MODEL = "auth.CustomUser"

You need to import your custom user model and not the original django.contrib.auth.models.User. 您需要导入自定义用户模型,而不是原始django.contrib.auth.models.User。

So it should work just with 所以它应该适用于

from django.contrib.auth.admin import UserAdmin
from myapp.models import CustomUser
admin.site.register(CustomUser, UserAdmin)

in your admin.py 在你的admin.py中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM