VUE3+Element plus实现django的管理后台的用户组管理

目录

需求

实现


需求

使用VUE实现django的后台页面时,修改用户组的原始后台界面如下​

实现

权限来自于django.contrib.auth.models的Permission模型,但是直接查询该模型时只能获取到如下结果。只有四个字段,id,name,codename,content_type,和后台界面显示的不完全相同​

from django.contrib.auth.models import Permission
tmp_permissions=Permission.objects.all()
permission_serializer = PermissionSerializer(tmp_permissions, many=True, context={'request': request})
print('tmp_permissions', permission_serializer.data)

查看Permission的模型定义,的确只有这4个字段,但是content_type 这个字段来自于ContentType模型​

from django.contrib.contenttypes.models import ContentType

class Permission(models.Model):
    """
    The permissions system provides a way to assign permissions to specific
    users and groups of users.

    The permission system is used by the Django admin site, but may also be
    useful in your own code. The Django admin site uses permissions as follows:

        - The "add" permission limits the user's ability to view the "add" form
          and add an object.
        - The "change" permission limits a user's ability to view the change
          list, view the "change" form and change an object.
        - The "delete" permission limits the ability to delete an object.
        - The "view" permission limits the ability to view an object.

    Permissions are set globally per type of object, not per specific object
    instance. It is possible to say "Mary may change news stories," but it's
    not currently possible to say "Mary may change news stories, but only the
    ones she created herself" or "Mary may only change news stories that have a
    certain status or publication date."

    The permissions listed above are automatically created for each model.
    """

    name = models.CharField(_("name"), max_length=255)
    content_type = models.ForeignKey(
        ContentType,
        models.CASCADE,
        verbose_name=_("content type"),
    )
    codename = models.CharField(_("codename"), max_length=100)

    objects = PermissionManager()

    class Meta:
        verbose_name = _("permission")
        verbose_name_plural = _("permissions")
        unique_together = [["content_type", "codename"]]
        ordering = ["content_type__app_label", "content_type__model", "codename"]

    def __str__(self):
        return "%s | %s" % (self.content_type, self.name)

    def natural_key(self):
        return (self.codename,) + self.content_type.natural_key()

    natural_key.dependencies = ["contenttypes.contenttype"]

检查ContentType的模型定义,发现有个app_labeled_name的属性,打印出来可以看到正是后台页面的每个记录的前半部分。后半部分是前面查询的name字段,这样就可以直接拼接这个数据返回到前端页面了​

class ContentType(models.Model):
    app_label = models.CharField(max_length=100)
    model = models.CharField(_("python model class name"), max_length=100)
    objects = ContentTypeManager()

    class Meta:
        verbose_name = _("content type")
        verbose_name_plural = _("content types")
        db_table = "django_content_type"
        unique_together = [["app_label", "model"]]

    def __str__(self):
        return self.app_labeled_name

    @property
    def name(self):
        model = self.model_class()
        if not model:
            return self.model
        return str(model._meta.verbose_name)

    @property
    def app_labeled_name(self):
        model = self.model_class()
        if not model:
            return self.model
        return "%s | %s" % (model._meta.app_label, model._meta.verbose_name)

    def model_class(self):
        """Return the model class for this type of content."""
        try:
            return apps.get_model(self.app_label, self.model)
        except LookupError:
            return None

    def get_object_for_this_type(self, **kwargs):
        """
        Return an object of this type for the keyword arguments given.
        Basically, this is a proxy around this object_type's get_object() model
        method. The ObjectNotExist exception, if thrown, will not be caught,
        so code that calls this method should catch it.
        """
        return self.model_class()._base_manager.using(self._state.db).get(**kwargs)

    def get_all_objects_for_this_type(self, **kwargs):
        """
        Return all objects of this type for the keyword arguments given.
        """
        return self.model_class()._base_manager.using(self._state.db).filter(**kwargs)

    def natural_key(self):
        return (self.app_label, self.model)

前端收到拼接后的数据,然后通过el-transfer组件展示即可​

【资源说明】 课程设计基于Vue3Django实现的rbac权限管理系统python源码(含前端)+项目说明.zip 在线预览 [https://xadmin.dvcloud.xin/](https://xadmin.dvcloud.xin/) 账号密码:admin/admin123 生成数据表并迁移 ```shell python manage.py makemigrations python manage.py migrate ``` 创建管理员账户 ```shell python manage.py createsuperuser ``` 启动程序 ## a.本地环境直接启动 ```shell python manage.py start all ``` ## b.容器化启动 ```shell docker compose up -d ``` 导入默认菜单 ```shell python manage.py loaddata loadjson/menu.json ``` # 附录 容器部署 ```shell docker compose up ``` 保存当前菜单为文件 ```shell python manage.py dumpdata system.MenuMeta system.Menu -o loadjson/menu.json ``` nginx 前端代理 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载,沟通交流,互相学习,共同进步!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘烟生活家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值