django admin中未显示自定义用户模型字段(AbstractUser) [英] Custom User model fields (AbstractUser) not showing in django admin

查看:369
本文介绍了django admin中未显示自定义用户模型字段(AbstractUser)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用AbstractUser方法扩展了django的User模型。问题是,我的自定义字段未显示在Django管理面板中。



我的models.py:



<$ p来自django.contrib.auth.models的$ p> import AbstractUser


class User(AbstractUser):
is_bot_flag = models.BooleanField(default = False )

我的管理员:

 从django.contrib.auth.admin导入UserAdmin 
从.models导入用户

admin.site.register(User,UserAdmin)

谢谢

解决方案

如果要查看自定义字段,还必须覆盖 UserAdmin 。文档中有一个此处的示例



您必须创建用于创建(以及更改)用户数据的表单,并覆盖 UserAdmin 。用于创建用户的表单为:

  class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label ='Password',widget = forms.PasswordInput)
password2 = Forms.CharField(label ='Password确认',widget = forms.PasswordInput)

类Meta:
模型=用户
字段='__all__'

def clean_password2(self):
password1 = self.cleaned_data.get( password1)
password2 = self.cleaned_data .get( password2)
,如果password1,password2和password1!= password2:
产生form.ValidationError( Passwords不匹配)
返回password2

def save(self,commit = True):
user = super()。save(commit = False)
user.set_password(self.cleaned_data [ password1])
如果提交:
user.save()
返回用户

您覆盖 UserAdmin 机智h:

 从django.contrib.auth.admin导入UserAdmin作为BaseUserAdmin 

类UserAdmin(BaseUserAdmin ):
add_form = UserCreationForm
add_fieldsets =(
(None,{
'classes':('wide',),
'fields':('email ','first_name','last_name','is_bot_flag','password1','password2')}
),

,然后您注册:

  admin.site.register(User, UserAdmin)

我几乎从文档中复制/粘贴了此内容,并删除了一些代码以使其更短。请转到文档查看完整的示例,包括用于更改用户数据的示例代码。


I have extended User model for django, using AbstractUser method. The problem is, my custom fields do not show in django admin panel.

My models.py:

from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    is_bot_flag = models.BooleanField(default=False)

My admin.py:

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

admin.site.register(User, UserAdmin)

Thanks

解决方案

You have to override UserAdmin as well, if you want to see your custom fields. There is an example here in the documentation.

You have to create the form for creating (and also changing) user data and override UserAdmin. Form for creating user would be:

class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = '__all__'

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user

You override UserAdmin with:

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

class UserAdmin(BaseUserAdmin):
    add_form = UserCreationForm
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'first_name', 'last_name', 'is_bot_flag', 'password1', 'password2')}
        ),
    )

and then you register:

admin.site.register(User, UserAdmin)

I pretty much copy/pasted this from documentation and deleted some code to make it shorter. Go to the documentation to see the full example, including example code for changing user data.

这篇关于django admin中未显示自定义用户模型字段(AbstractUser)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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