• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Django用户认证系统 组与权限解析

python 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

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’)


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Django用户认证系统 组与权限解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址