Django admin后台创建密文密码
如题现在有一张用户表User
# user/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
SEX_CHOICES = [
(0, '男'),
(1, '女'),
]
sex = models.IntegerField(choices=SEX_CHOICES, default=0, verbose_name='性别', help_text='性别')
email = models.EmailField(verbose_name='邮箱', help_text='邮箱',null=True, blank=True)
icon = models.ImageField(upload_to='icon', default='icon/default.png', verbose_name='头像', help_text='头像')
level = models.ForeignKey('permission.Level', on_delete=models.CASCADE, verbose_name='用户权限',
help_text='用户权限', null=True, blank=True)
job = models.ForeignKey('permission.Job', on_delete=models.CASCADE, verbose_name='岗位', help_text='岗位',
null=True, blank=True)
dept = models.ManyToManyField('permission.Dept', blank=True, related_name='user', verbose_name='该用户可查看部门',
help_text='该用户可查看部门')
menus = models.ManyToManyField('permission.Menu', blank=True, related_name='user', verbose_name='该用户可查看菜单',
help_text='该用户可查看菜单')
class Meta:
db_table = 'user'
verbose_name = '用户'
verbose_name_plural = verbose_name
def __str__(self):
return self.username
# admin.py
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from .models import User
from django.contrib.auth.admin import UserAdmin
@admin.register(User)
class UserAdmin(UserAdmin):
list_display = ('id', 'username', 'sex', 'email', 'icon', 'job')
list_filter = ('dept', 'menus')
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'sex', 'icon', 'job', 'dept', 'menus')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
- 现在的创建用户页面
- 现在的创建用户页面
[外链图片转存中…(img-EOwkRiyW-1717068231275)]