from django.contrib.auth.models import AbstractUser from django.db import models from ERP_5.utils.base_model import BaseModel class Menu(BaseModel): """菜单功能的模型类""" number = models.IntegerField('排序数字', blank=True, null=True) url = models.CharField('菜单访问的URL', max_length=256, blank=True, null=True) name = models.CharField('菜单名字', max_length=50) # 父菜单对象:parent parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE) class Meta: db_table = 't_menu' verbose_name = '功能菜单' verbose_name_plural = verbose_name ordering = ['number'] def __str__(self): return self.name # ERP系统的操作用户模型类,默认是采用django自带的User,需要配置settings class UserModel(AbstractUser): phone = models.CharField('手机号码', max_length=11, unique=True, blank=True, null=True) real_name = models.CharField('真实姓名', max_length=128, blank=True, null=True) roles = models.ManyToManyField('RolesModel', db_table='t_users_roles', blank=True, verbose_name='用户所拥有的角色列表') dept = models.ForeignKey('DeptModel', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='用户所在的部门') class Meta: db_table = 't_user' verbose_name = '功能菜单' verbose_name_plural = verbose_name ordering = ['id'] def __str__(self): return self.username class PermissionsModel(BaseModel): """ 权限模型 """ method_choices = ( ('POST', '增'), ('DELETE', '删'), ('PUT', '改'), ('PATCH', '改局部'), ('GET', '查') ) """ 只要是choices参数的字段 如果你想要获取对应信息 固定写法 get_字段名_display() print(permission.method) print(permission.get_method_display()) """ name = models.CharField(max_length=50, verbose_name='权限名') is_menu = models.BooleanField(verbose_name='是否为菜单', default=True) # True为菜单,False为接口 # 操作 , 如果是菜单(非接口),method method = models.CharField(max_length=8, blank=True, default='', choices=method_choices, verbose_name='请求方法') path = models.CharField(max_length=200, blank=True, default='', verbose_name='请求路径') # 资源 menu = models.ForeignKey('Menu', null=True, blank=True, related_name='permissions_list', on_delete=models.CASCADE) desc = models.CharField(max_length=512, blank=True, default='', verbose_name='权限描述') def __str__(self): return self.name class Meta: db_table = 't_permissions' verbose_name = '权限' verbose_name_plural = verbose_name ordering = ['id'] class RolesModel(BaseModel): """ 角色 """ name = models.CharField(max_length=32, unique=True, verbose_name='角色名字') permissions = models.ManyToManyField('PermissionsModel', db_table='t_roles_permissions', blank=True, verbose_name='权限') # 注意在用户模型中最好也要加ManyToManyField def __str__(self): return self.name class Meta: db_table = 't_roles' verbose_name = '角色' verbose_name_plural = verbose_name class DeptModel(BaseModel): """ 组织架构 部门 """ name = models.CharField(max_length=32, unique=True, verbose_name='部门') parent = models.ForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.CASCADE, verbose_name='父部门') address = models.CharField(max_length=128, blank=True, default='', verbose_name='部门地址') def __str__(self): return self.name class Meta: db_table = 't_dept' verbose_name = '部门' verbose_name_plural = verbose_name