标签:auth __str__ meta 用户 ural turn views length class
导入django自带的用户基类和分组模型
from django.contrib.auth.models import AbstractUser, Group
创建模型
class DongYaoUser(AbstractUser):
name = models.CharField(max_length=32, verbose_name=‘姓名‘)
department = models.ManyToManyField(‘Role‘, verbose_name=‘职位‘)
class Meta:
verbose_name_plural = ‘员工管理‘
def __str__(self):
return self.name
class Role(models.Model):
name = models.CharField(max_length=32, verbose_name=‘职位名称‘)
#多对多关联group模型
groups = models.ManyToManyField(to=Group, verbose_name="权限组")
class Meta:
verbose_name_plural = ‘职位管理‘
def __str__(self):
return self.name
views.py
#从角色中取出该角色的所有权限组并去重
role_list = staff.role.all()
group_set = {group.id for role in role_list for group in role.groups.all()}
#给角色添加权限
staff.groups.add(*group_set)
标签:auth __str__ meta 用户 ural turn views length class
原文地址:https://www.cnblogs.com/louzi/p/9897422.html