设为首页 加入收藏

TOP

Django用户认证系统(三)组与权限(三)
2019-02-12 16:07:55 】 浏览:222
Tags:Django 用户 认证 系统 权限
rm('foo.can_vote')

默认权限default permissions

如果已经在 INSTALLED_APPS配置了django.contrib.auth,它会保证为installed applications中的每个Django Model创建3个缺省权限:add, change 和 delete。

这些权限会在你第一次运行 manage.py migrate(1.7之前为syncdb) 时创建。当时所有的models都会建立权限。在这之后创建的新models会在再次运行 manage.py migrate时创建这些默认权限。这些权限与admin管理界面中的创建,删除,修改行为是一一对应的。

假设你有一个应用 foo ,其中有一个模型 Bar, 你可以用下述方法来测试基本权限:

add: user.has_perm('foo.add_bar')
change: user.has_perm('foo.change_bar')
delete: user.has_perm('foo.delete_bar')
权限模型( Permission model)一般不直接使用。

组Groups

组也是作为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
@python_2_unicode_compatible
class  Group(models.Model):
     """
     Groups are a generic way of categorizing users to apply permissions, or
     some other label, to those users. A user can belong to any number of
     groups.
 
     A user in a group automatically has all the permissions granted to that
     group. For example, if the group Site editors has the permission
     can_edit_home_page, any user in that group will have that permission.
 
     Beyond permissions, groups are a convenient way to categorize users to
     apply some label, or extended functionality, to them. For example, you
     could create a group 'Special users', and you could write code that would
     do special things to those users -- such as giving them access to a
     members-only portion of your site, or sending them members-only email
     messages.
     """
     name  =  models.CharField(_( 'name' ), max_length = 80 , unique = True )
     permissions  =  models.ManyToManyField(Permission,
         verbose_name = _( 'permissions' ), blank = True )
 
     objects  =  GroupManager()
 
     class  Meta:
         verbose_name  =  _( 'group' )
         verbose_name_plural  =  _( 'groups' )
 
     def  __str__( self ):
         return  self .name
 
     def  natural_key( self ):
         return  ( self .name,)

字段fields:

name:必需,80个字符或更少,例如, 'Awesome Users'。

permissions:ManyToManyField to Permission

1
2
3
4
group.permissions  =  [permission_list]
group.permissions.add(permission, permission, ...)
group.permissions.remove(permission, permission, ...)
group.permissions.clear()

Programmatica

首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇TensorFlow数据集(二)——数据.. 下一篇扣丁学堂Python在线学习简述Pytho..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目