Django 匿名用户AnonymousUser

源码

from django.contrib.auth.models import AnonymousUser

class AnonymousUser:

id = None

pk = None

username = ''

is_staff = False

is_active = False

is_superuser = False

_groups = EmptyManager(Group)

_user_permissions = EmptyManager(Permission)


def __str__(self):

return 'AnonymousUser'


def __eq__(self, other):

return isinstance(other, self.__class__)


def __hash__(self):

return 1 # instances always return the same hash value


def save(self):

raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")


def delete(self):

raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")


def set_password(self, raw_password):

raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")



def check_password(self, raw_password):

raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")



@property

def groups(self):

return self._groups


@property

def user_permissions(self):

return self._user_permissions


def get_group_permissions(self, obj=None):

return set()


def get_all_permissions(self, obj=None):

return _user_get_all_permissions(self, obj=obj)


def has_perm(self, perm, obj=None):

return _user_has_perm(self, perm, obj=obj)


def has_perms(self, perm_list, obj=None):

return all(self.has_perm(perm, obj) for perm in perm_list)


def has_module_perms(self, module):

return _user_has_module_perms(self, module)


@property

def is_anonymous(self):

return True


@property

def is_authenticated(self):

return False


def get_username(self):

return self.username

AnonymousUser对象¶

class models.AnonymousUser

django.contrib.auth.models.AnonymousUser是一个实现django.contrib.auth.models.User接口的类,具有以下差异:

  • id始终是None
  • username 永远是空字符串。
  • get_username() 总是返回空字符串。
  • is_anonymousTrue 不是False
  • is_authenticated是 False不是True
  • is_staff并且 is_superuser永远 False
  • is_active永远False
  • groups并且 user_permissions总是空的。
  • set_password(), check_password(), save()和 delete()提高NotImplementedError

你可能感兴趣的:(Djano)