class guardian.core.ObjectPermissionChecker(user_or_group=None)

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


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

Returns cache key for _obj_perms_cache dict.


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.

  • 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

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

Parameters:objects – Iterable of Django model objects