Django的权限系统很简单,它可以赋予users或groups中的users以权限。
Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中。
User对象具有两个ManyToManyField字段,groups和user_permissions
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一样来访问他们:
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的实例。
@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 <div>本文来源gaodai.ma#com搞#代!码网_</div>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. 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." Three basic permissions -- add, change and delete -- are automatically created for each Django model. """ name = models.CharField(_('name'), max_length=255) content_type = models.ForeignKey(ContentType) 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 | %s" % ( six.text_type(self.content_type.app_label), six.text_type(self.content_type), six.text_type(self.name)) def natural_key(self): return (self.codename,) + self.content_type.natural_key() natural_key.dependencies = ['contenttypes.contenttype']
字段fields
name:必需。50个字符或更少,例如,’Can Vote‘
content_type:必需,一个对于django_content_type数据库table的引用,table中含有每个应用中的Model的记录。
codename:必需,100个字符或更少,例如,’can_vote’。
如果要为某个Model创建权限:
from django.db import models class Vote(models.Model): ... class Meta: permissions = (("can_vote", "Can Vote"),)
如果这个Model在应用foo中,则权限表示为’foo.can_vote’,检查某个用户是否具有权限myuser.has_perm(‘foo.can_vote’)