Django自定义用户模型显示纯文本的密码字段

时间:2016-07-29 10:49:28

标签: python django

我扩展了django用户模型并使用 AbstractBaseUser 方法对其进行了自定义。现在在管理页面密码字段中显示密码为纯文本。我怎么解决这个?我需要一个答案,如何在不实现自定义表单的情况下解决这个问题。

model.py

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(_('username'), max_length = 30, unique = True,
                                help_text = _('Required. 30 characters or fewer. Letters, numbers and '
                                              '@/./+/-/_ characters'),
                                validators = [
                                    validators.RegexValidator(re.compile(
                                        '^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
                                ])
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_active = models.BooleanField(_('active'), default = True,
                                    help_text = _('Designates whether this user should be treated as '
                                                  'active. Unselect this instead of deleting accounts.'))
    is_staff = models.BooleanField(_('staff status'), default = False,
                                   help_text = _('Designates whether the user can log into this admin '
                                                 'site.'))
    is_admin = models.BooleanField(default = False)

    date_joined = models.DateTimeField(_('date joined'), default = timezone.now)
    # avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
    age = models.IntegerField(_('age'), default = 0)
    address = models.CharField(_('address'), max_length = 100,blank = True)
    user_type = models.ForeignKey( UserType , default = '0',)

    objects = UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        '''
        Returns the first_name plus the last_name, with a space in between.
        '''
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        '''
        Returns the short name for the user.
        '''
        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):
        '''
        Sends an email to this User.
        '''
        send_mail(subject, message, from_email, [self.email], **kwargs)

admin.py

class UserAdmin(admin.ModelAdmin):

    class Meta:
        models = User

    fieldsets = (
            (None, {'fields': ('username', 'password')}),
            (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'address', 'age')}),
            (_('Permissions'), {'fields': ('user_type','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','email','address', 'age', 'user_type')}
             ),
        )
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff','user_type')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('groups', 'user_permissions',)

1 个答案:

答案 0 :(得分:0)

它对我有用,导入BaseUserAdmin

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


class UserAdmin(BaseUserAdmin):
    search_fields = ['username', 'first_name', 'last_name',]
    list_display = ('username', 'first_name', 'last_name',)
    filter_horizontal = ('groups', 'user_permissions')
    fieldsets = (
        ('Usuario', {'fields': ('username', 'password')}),
        ('Información Personal', {'fields': (
            'first_name',
            'last_name',
            'email',
        )}),
        ('Permisos', {'fields': (
            'is_active',
            'is_staff',
            'is_superuser',
            'groups',
            'user_permissions'
        )}),
    )

admin.site.register(User, UserAdmin)