将 Django auth UserAdmin 用于自定义用户模型 [英] Using Django auth UserAdmin for a custom user model

查看:34
本文介绍了将 Django auth UserAdmin 用于自定义用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Django.Contrib.Auth 文档:

扩展 Django 的默认用户如果您对 Django 的 User 模型完全满意,并且只想添加一些额外的配置文件信息,您可以简单地将 django.contrib.auth.models.AbstractUser 子类化并添加您的自定义配置文件字段.此类提供了作为抽象模型的默认用户的完整实现.

Extending Django’s default User 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.

说和做.我创建了一个如下所示的新模型:

Said and done. I created a new model like below:

class MyUser(AbstractUser):
  some_extra_data = models.CharField(max_length=100, blank=True)

这几乎就像 Django 的标准 User 一样出现在 admin 中.但是,admin 中最重要的区别是密码(重新)设置字段不存在,而是显示了一个普通的 CharField.我真的必须覆盖 admin-config 中的东西才能让它工作吗?如果是这样,我怎么能以有点 DRY 的方式做到这一点(即不从 Django 源代码中复制东西...... eww......)?

This shows up in admin almost like Django's standard User. However, the most important difference in admin is that the password-(re)set field is not present, but a normal CharField is displayed instead. Do I really have to override stuff in the admin-config to get this to work? If so, how can I do that in somewhat DRY way (i.e. without copying stuff from the Django source... eww...)?

推荐答案

在对 Django 源代码进行了一段时间的挖掘后,我找到了一个可行的解决方案.我对这个解决方案并不完全满意,但它似乎有效.欢迎提出更好的解决方案!

After digging around the Django source code for a while, I found a working soultion. I am not totally happy with this solution, but it seems to work. Feel free to suggest better solutions!

Django 使用 UserAdminUser 模型呈现漂亮的管理员外观.通过在我们的 admin.py 文件中使用它,我们可以获得与我们的模型相同的外观.

Django uses UserAdmin to render the nice admin look for User model. By just using this in our admin.py-file, we can get the same look for our model.

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

然而,仅此一项可能不是一个好的解决方案,因为 Django Admin 不会显示您的任何特殊字段.这有两个原因:

However, this alone is probably not a good solution, since Django Admin will not display any of your special fields. There are two reasons for this:

  • UserAdmin 使用 UserChangeForm 作为修改对象时使用的表单,而对象又使用 User 作为其模型.莉>
  • UserAdmin 定义了一个 formsets 属性,稍后由 UserChangeForm 使用,其中不包括您的特殊字段.
  • UserAdmin uses UserChangeForm as the form to be used when modifying the object, which in its turn uses User as its model.
  • UserAdmin defines a formsets-property, later used by UserChangeForm, which does not include your special fields.

因此,我创建了一个特殊的变更表单,它重载了 Meta 内部类,以便变更表单使用正确的模型.我还必须重载 UserAdmin 以将我的特殊字段添加到字段集,这是我有点不喜欢这个解决方案的一部分,因为它看起来有点难看.随时提出改进建议!

So, I created a special change-form which overloads the Meta inner-class so that the change form uses the correct model. I also had to overload UserAdmin to add my special fields to the fieldset, which is the part of this solution I dislike a bit, since it looks a bit ugly. Feel free to suggest improvements!

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

class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = MyUser

class MyUserAdmin(UserAdmin):
    form = MyUserChangeForm

    fieldsets = UserAdmin.fieldsets + (
            (None, {'fields': ('some_extra_data',)}),
    )


admin.site.register(MyUser, MyUserAdmin)

这篇关于将 Django auth UserAdmin 用于自定义用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆