VUE3+Element plus实现django的管理后台的用户组管理
权限来自于django.contrib.auth.models的Permission模型,但是直接查询该模型时只能获取到如下结果。只有四个字段,id,name,codename,content_type,和后台界面显示的不完全相同。检查ContentType的模型定义,发现有个app_labeled_name的属性,打印出来可以看到正是后台页面的每个记录的前半部分。查看Permission的模型
·
目录
需求
使用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组件展示即可
更多推荐
已为社区贡献1条内容
所有评论(0)