Django之自定义权限

Posted 孔扎根

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django之自定义权限相关的知识,希望对你有一定的参考价值。

官方解释

Custom permissions?

To create custom permissions for a given model object, use the permissions model Meta attribute.

This example Task model creates three custom permissions, i.e., actions users can or cannot do with Task instances, specific to your application:

class Task(models.Model):
    ...
    class Meta:
        permissions = (
            ("view_task", "Can see available tasks"),
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        )

The only thing this does is create those extra permissions when you run manage.py migrate (the function that creates permissions is connected to the post_migrate signal). Your code is in charge of checking the value of these permissions when a user is trying to access the functionality provided by the application (viewing tasks, changing the status of tasks, closing tasks.) Continuing the above example, the following checks if a user may view tasks:

user.has_perm(‘app.view_task‘)

 

项目实战

 

以上是关于Django之自定义权限的主要内容,如果未能解决你的问题,请参考以下文章

Django模板渲染之自定义inclusion_tag详细使用

Django之自定义标签,过滤器,以及inclusion_tag

Django 表单验证之自定义表单验证器

DJANGO之自定义模板过滤器

Django入门之自定义页面

Django 模板之自定义函数