is_staff字段未在Django管理中显示

时间:2019-03-22 00:53:05

标签: django django-models django-views django-admin django-custom-user

is_staff被定义为布尔字段,但其复选框在管理页面中不可见。但是is_admin是可见的,可以更改。

我无法使用views.py

对该字段进行更改
class Users(AbstractBaseUser, PermissionsMixin):
    objects = UserManager()
    mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True)
    email = models.EmailField(_('Email'), max_length=75, null=False, blank=False)
    first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True)
    last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True)
    role = models.CharField(_('Role'), max_length=70, null=True, blank=True)
    location = models.CharField(_('Location'), max_length=70, null=True, blank=True)
    date_time = models.DateTimeField(_('DateTime'), auto_now=True, null=True, blank=True)
    activated = models.BooleanField(_('Activated'), default=False)
    is_admin = models.BooleanField(_('is_admin'), default=False)
    is_staff = models.BooleanField(_('is_staff'), default=False)

    def __unicode__(self):
        return str(self.mobile_no)

    def __str__(self):
        return str(self.mobile_no)

    def get_full_name(self):
        return self.first_name + " " + self.last_name

    class Meta:
        ordering = ['-id']

    @property
    def is_staff(self):
        return self.is_admin


    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return self.is_admin

    USERNAME_FIELD = 'mobile_no'
    REQUIRED_FIELDS = ['role']

1 个答案:

答案 0 :(得分:0)

您可以通过导入admin.py并从中继承来编辑User admin。这是一个示例:

# admin.py

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

class UserAdmin(BaseUserAdmin):
    model = Users
    list_display = ('mobile_no', 'email', 'first_name', 'last_name', 'role', 'location')
    fieldsets = (
        (('Personal info'), {'fields': ('first_name', 'last_name',)}),
        (('Permissions'), {'fields': ('activated', 'is_staff', 'is_superuser', 'groups')}),
        (('Important dates'), {'fields': ('date_time', 'last_login')}),
    )
    class Meta:
        model = User

admin.site.register(User, UserAdmin)