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

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

问题描述

Django.Contrib .Auth文档


扩展Django的默认用户
如果您完全很高兴与Django的用户模型,您只需要添加一些额外的配置文件信息,您可以简单地子类 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的标准用户。但是,管理员最重要的区别是密码(重设)字段不存在,而是显示正常的CharField。我真的要覆盖admin-config中的内容才能使其工作吗?如果是这样的话,我该怎么做才能做到这一点呢(比如没有从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使用 UserAdmin 呈现不错的管理员查找用户模型。只需在我们的 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 定义了一个 formets -property,稍后由 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.

所以,我创建了一个特殊的变更格式,以使变更表使用正确的模型。我也不得不重载 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天全站免登陆