Shortcuts — django-guardian v0.1.1 documentation

### Navigation

- index
- modules |
- next |
- previous |
- django-guardian v0.1.1 documentation »
- API Reference »

# Shortcuts

Convenient shortcuts to manage or check object permissions.

Return to API Reference.

## assign

`guardian.shortcuts.` `assign`(perm, user_or_group, obj)

  Assigns permission to user/group and object pair.

  Parameters: | perm – proper permission for given `obj`, as string (codename) user_or_group – instance of `User`, `AnonymousUser` or `Group`; passing any other object would raise `guardian.exceptions.NotUserNorGroup` exception obj – persisted Django’s `Model` instance

  We can assign permission for `Model` instance for specific user:

      >>> from django.contrib.sites.models import Site
      >>> from django.contrib.auth.models import User, Group
      >>> from guardian.shortcuts import assign
      >>> site = Site.objects.get_current()
      >>> user = User.objects.create(username='joe')
      >>> assign("change_site", user, site)
      <UserObjectPermission: example.com | joe | change_site>
      >>> user.has_perm("change_site", site)
      True

  ... or we can assign permission for group:

      >>> group = Group.objects.create(name='joe-group')
      >>> user.groups.add(group)
      >>> assign("delete_site", group, site)
      <GroupObjectPermission: example.com | joe-group | delete_site>
      >>> user.has_perm("delete_site", site)
      True

## remove_perm

`guardian.shortcuts.` `remove_perm`(perm, user_or_group=None, obj=None)

  Removes permission from user/group and object pair.

## get_perms

`guardian.shortcuts.` `get_perms`(user_or_group, obj)

  Returns permissions for given user/group and object pair, as list of strings.

## get_perms_for_model

`guardian.shortcuts.` `get_perms_for_model`(cls)

  Returns queryset of all Permission objects for the given class. It is possible to pass Model as class or instance.

### Navigation

- index
- modules |
- next |
- previous |
- django-guardian v0.1.1 documentation »
- API Reference »