Django自定义用户管理页面

时间:2017-12-30 08:27:20

标签: python django

我目前正在尝试为我制作的自定义用户模型实现管理页面。我添加了一个名为“email_confirmed”的字段,我试图将其显示在“管理员”页面上。

这是模型:

class User(AbstractUser):
    """User model."""
    abstract = True
    username = None
    email = EmailField(_('email address'), unique=True)
    email_confirmed = BooleanField(default = False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'second_name']

    objects = UserManager()

    def get_full_name(self):
        fname = self.first_name
        lname = self.last_name
        return '{} {}'.format(fname, lname)

    def email_confirmed(self):
        return self.email_confirmed

正如您所看到的,我尝试编写一种名为“电子邮件”的方法,以确保其正常运行,但它无效。

这是我在admin.py中的UserAdmin:

@admin.register(get_user_model())
class UserAdmin(UserAdmin):
    class Meta:

        model = get_user_model()

    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email_confirmed')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2', 'email_confirmed()'),
        }),
    )
    list_display = ('email', 'first_name', 'last_name', 'is_staff', 'email_confirmed')
    search_fields = ('email', 'first_name', 'last_name', 'email_confirmed')
    ordering = ('email',)

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

不要写这个方法

def email_confirmed(self):
    return self.email_confirmed