Core

ObjectPermissionChecker

class guardian.core.ObjectPermissionChecker(user_or_group=None)

Generic object permissions checker class being the heart of django-guardian.

Note

Once checked for single object, permissions are stored and we don’t hit database again if another check is called for this object. This is great for templates, views or other request based checks (assuming we don’t have hundreds of permissions on a single object as we fetch all permissions for checked object).

On the other hand, if we call has_perm for perm1/object1, then we change permission state and call has_perm again for same perm1/object1 on same instance of ObjectPermissionChecker we won’t see a difference as permissions are already fetched and stored within cache dictionary.

Constructor for ObjectPermissionChecker.

Parameters:user_or_group – should be an User, AnonymousUser or Group instance
get_local_cache_key(obj)

Returns cache key for _obj_perms_cache dict.

get_perms(obj)

Returns list of codename’s of all permissions for given obj.

Parameters:obj – Django model instance for which permission should be checked
has_perm(perm, obj)

Checks if user/group has given permission for object.

Parameters:
  • perm – permission as string, may or may not contain app_label prefix (if not prefixed, we grab app_label from obj)
  • obj – Django model instance for which permission should be checked
prefetch_perms(objects)

Prefetches the permissions for objects in objects and puts them in the cache.

Parameters:objects – Iterable of Django model objects