其他分享
首页 > 其他分享> > 【Django admin】继承并扩展 AbstractUser, Group

【Django admin】继承并扩展 AbstractUser, Group

作者:互联网

# models.py   

from django.db import models

from django.contrib.auth.models import AbstractUser,Group,Permission # 这里也可以扩展权限表 就不演示了


# 继承admin用户表 并且扩展
class AdminUser(AbstractUser):

    admin_name = models.CharField(max_length=25,verbose_name="昵称",null=True,blank=True)

    # def __str__(self):
    #     return self.username

    class Meta:
        db_table = 'auth_admin_user'
        verbose_name_plural = "管理员信息"


# 继承admin 分组表
class AdminGroup(Group):

    roles = models.CharField(max_length=25,verbose_name="角色",null=True,blank=True)

    # def __str__(self):
    #     return self.roles

    class Meta:
        db_table = 'auth_admin_roles'
        verbose_name_plural = "角色信息"

 

 

 

 

# admin.py 

from django.contrib import admin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm, AdminPasswordChangeForm
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Group, Permission

from app.custom_auth.models import AdminUser,AdminGroup


# django/contrib/auth/admin/UserAdmin  这里的ADMIN源码直接搬过来进行配置

# 重写用户展示字段
@admin.register(AdminUser)
class UserAdmin(admin.ModelAdmin):

    add_form_template = 'admin/auth/user/add_form.html'
    change_user_password_template = None
    fieldsets = (
        # 管理员界面  第一板块展示
        (None, {'fields': ('username', 'password',)}),
        # 管理员界面  第二板块展示
        (('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'admin_name')}),
        # 管理员界面  第三板块展示
        (('Permissions'), {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
        }),
        (('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )

    form = UserChangeForm
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff','admin_name',)
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('groups', 'user_permissions',)



# 重写组admin
@admin.register(AdminGroup)
class GroupAdmin(admin.ModelAdmin):

    search_fields = ('roles',)
    list_display = ('roles',)
    ordering = ('roles',)
    filter_horizontal = ('permissions',)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        if db_field.name == 'permissions':
            qs = kwargs.get('queryset', db_field.remote_field.model.objects)
            # Avoid a major performance hit resolving permission names which
            # triggers a content_type load:
            kwargs['queryset'] = qs.select_related('content_type')
        return super().formfield_for_manytomany(db_field, request=request, **kwargs)

 

 

# settings.py

INSTALLED_APPS = [

    'app.custom_auth', # 这是我的应用名称

]


# 用于外键
AUTH_USER_MODEL = 'custom_auth.AdminUser'

 

标签:Group,name,admin,db,auth,AbstractUser,models,import
来源: https://www.cnblogs.com/wanghong1994/p/16456795.html