设为首页 加入收藏

TOP

Django用户认证系统(三)组与权限(一)
2019-02-12 16:07:55 】 浏览:31
Tags:Django 用户 认证 系统 权限

原文:https://www.cnblogs.com/linxiyue/p/4060817.html

Django的权限系统很简单,它可以赋予users或groups中的users以权限。

Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中。

User对象具有两个ManyToManyField字段,groups和user_permissions

1
2
3
4
5
6
7
8
9
groups  =  models.ManyToManyField(Group, verbose_name = _( 'groups' ),
     blank = True , help_text = _( 'The groups this user belongs to. A user will '
                             'get all permissions granted to each of '
                             'their groups.' ),
     related_name = "user_set" , related_query_name = "user" )
user_permissions  =  models.ManyToManyField(Permission,
     verbose_name = _( 'user permissions' ), blank = True ,
     help_text = _( 'Specific permissions for this user.' ),
     related_name = "user_set" , related_query_name = "user" )

可以像其它的django Model一样来访问他们:

1
2
3
4
5
6
7
8
myuser.groups  =  [group_list]
myuser.groups.add(group, group, ...)
myuser.groups.remove(group, group, ...)
myuser.groups.clear()
myuser.user_permissions  =  [permission_list]
myuser.user_permissions.add(permission, permission, ...)
myuser.user_permissions.remove(permission, permission, ...)
myuser.user_permissions.clear()

权限Permissions

权限是作为一个Model存在的,建立一个权限就是创建一个Permission Model的实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@python_2_unicode_compatible
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.
 
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇TensorFlow数据集(二)——数据.. 下一篇扣丁学堂Python在线学习简述Pytho..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }