| 1 |
--- |
| 2 |
title: Shortcuts |
| 3 |
description: Understanding permission query functions and their relationships. |
| 4 |
--- |
| 5 |
|
| 6 |
# Permission Query Functions |
| 7 |
|
| 8 |
Django Guardian provides several shortcut functions for querying permissions. Understanding their relationships and differences is crucial for proper usage. |
| 9 |
|
| 10 |
## Permission Query Functions Overview |
| 11 |
|
| 12 |
When working with object permissions in django-guardian, there are three main functions for querying permissions: |
| 13 |
|
| 14 |
### Function Relationships |
| 15 |
|
| 16 |
- **`get_perms(user, obj)`**: Returns ALL permissions (user + group permissions combined) |
| 17 |
- **`get_user_perms(user, obj)`**: Returns ONLY direct user permissions |
| 18 |
- **`get_group_perms(user, obj)`**: Returns ONLY group permissions |
| 19 |
|
| 20 |
For any user and object: `get_perms(user, obj) = get_user_perms(user, obj) + get_group_perms(user, obj)` |
| 21 |
|
| 22 |
### Key Differences |
| 23 |
|
| 24 |
| Function | Returns | Type | Includes Groups | Use Case | |
| 25 |
|----------|---------|------|----------------|----------| |
| 26 |
| `get_perms()` | All permissions | `list[str]` | Yes | General permission checking | |
| 27 |
| `get_user_perms()` | Direct user permissions only | `QuerySet` | No | Permission management interfaces | |
| 28 |
| `get_group_perms()` | Group permissions only | `QuerySet` | N/A | Understanding permission inheritance | |
| 29 |
|
| 30 |
## Important Notes |
| 31 |
|
| 32 |
- **Inactive users** (`is_active=False`): All functions return empty results |
| 33 |
- **Return types**: `get_perms()` returns a list, while `get_user_perms()` and `get_group_perms()` return QuerySets |
| 34 |
- **Group behavior**: When `get_group_perms()` is called with a user, it returns permissions from ALL groups the user belongs to |
| 35 |
- **Superusers**: `get_perms()` returns all available permissions for the object's model |
| 36 |
|
| 37 |
## Common Use Cases |
| 38 |
|
| 39 |
### General Permission Checking |
| 40 |
|
| 41 |
Use `get_perms()` when you need to check if a user has a specific permission, regardless of whether it comes from direct assignment or group membership: |
| 42 |
|
| 43 |
```python |
| 44 |
from guardian.shortcuts import get_perms |
| 45 |
|
| 46 |
user_perms = get_perms(user, article) |
| 47 |
if 'change_article' in user_perms: |
| 48 |
# User can change this article (either directly or through groups) |
| 49 |
allow_editing = True |
| 50 |
``` |
| 51 |
|
| 52 |
### Permission Management Interfaces |
| 53 |
|
| 54 |
Use `get_user_perms()` and `get_group_perms()` when building interfaces that show the source of permissions: |
| 55 |
|
| 56 |
```python |
| 57 |
from guardian.shortcuts import get_user_perms, get_group_perms |
| 58 |
|
| 59 |
# Show user what permissions they have directly |
| 60 |
direct_perms = get_user_perms(user, article) |
| 61 |
|
| 62 |
# Show user what permissions they have through groups |
| 63 |
group_perms = get_group_perms(user, article) |
| 64 |
|
| 65 |
# Display in UI: "You have 'edit' permission directly, and 'delete' through the 'Editors' group" |
| 66 |
``` |
| 67 |
|
| 68 |
### Permission Auditing |
| 69 |
|
| 70 |
Use individual functions to understand permission inheritance: |
| 71 |
|
| 72 |
```python |
| 73 |
from guardian.shortcuts import get_perms, get_user_perms, get_group_perms |
| 74 |
|
| 75 |
all_perms = get_perms(user, document) |
| 76 |
direct_perms = list(get_user_perms(user, document).values_list('codename', flat=True)) |
| 77 |
group_perms = list(get_group_perms(user, document).values_list('codename', flat=True)) |
| 78 |
|
| 79 |
print(f"All permissions: {all_perms}") |
| 80 |
print(f"Direct permissions: {direct_perms}") |
| 81 |
print(f"Group permissions: {group_perms}") |
| 82 |
``` |
| 83 |
|
| 84 |
### Selective Permission Removal |
| 85 |
|
| 86 |
Use `get_user_perms()` when you need to remove only direct permissions without affecting group permissions: |
| 87 |
|
| 88 |
```python |
| 89 |
from guardian.shortcuts import get_user_perms, remove_perm |
| 90 |
|
| 91 |
# Remove only direct user permissions, keep group permissions intact |
| 92 |
direct_perms = get_user_perms(user, article) |
| 93 |
for perm in direct_perms: |
| 94 |
remove_perm(perm.codename, user, article) |
| 95 |
``` |
| 96 |
|
| 97 |
## Common Pitfalls |
| 98 |
|
| 99 |
!!! warning "Don't Confuse get_user_perms() with get_perms()" |
| 100 |
|
| 101 |
Many developers expect `get_user_perms()` to return the same results as `get_perms()`, but this is incorrect: |
| 102 |
|
| 103 |
```python |
| 104 |
# This user has permissions through group membership only |
| 105 |
user.groups.add(editors_group) # editors_group has 'change_article' permission |
| 106 |
|
| 107 |
get_perms(user, article) # Returns: ['change_article'] |
| 108 |
get_user_perms(user, article) # Returns: [] (empty - no direct permissions) |
| 109 |
``` |
| 110 |
|
| 111 |
!!! tip "Use get_perms() for Most Cases" |
| 112 |
|
| 113 |
Unless you specifically need to distinguish between direct and inherited permissions, use `get_perms()` for general permission checking. |
| 114 |
|
| 115 |
## Examples |
| 116 |
|
| 117 |
### Basic Usage |
| 118 |
|
| 119 |
```python |
| 120 |
from django.contrib.auth.models import User |
| 121 |
from guardian.shortcuts import assign_perm, get_perms, get_user_perms, get_group_perms |
| 122 |
from myapp.models import Article |
| 123 |
|
| 124 |
user = User.objects.get(username='john') |
| 125 |
article = Article.objects.get(pk=1) |
| 126 |
|
| 127 |
# Assign direct permission |
| 128 |
assign_perm('change_article', user, article) |
| 129 |
|
| 130 |
# Check all permissions (includes direct + group) |
| 131 |
all_perms = get_perms(user, article) # ['change_article'] |
| 132 |
|
| 133 |
# Check only direct permissions |
| 134 |
direct_perms = get_user_perms(user, article) # QuerySet with 'change_article' |
| 135 |
|
| 136 |
# Check only group permissions |
| 137 |
group_perms = get_group_perms(user, article) # Empty QuerySet |
| 138 |
``` |
| 139 |
|
| 140 |
### Group Permissions Example |
| 141 |
|
| 142 |
```python |
| 143 |
from django.contrib.auth.models import User, Group |
| 144 |
from guardian.shortcuts import assign_perm, get_perms, get_user_perms, get_group_perms |
| 145 |
|
| 146 |
user = User.objects.get(username='jane') |
| 147 |
group = Group.objects.create(name='editors') |
| 148 |
article = Article.objects.get(pk=1) |
| 149 |
|
| 150 |
# Add user to group and assign group permission |
| 151 |
user.groups.add(group) |
| 152 |
assign_perm('delete_article', group, article) |
| 153 |
|
| 154 |
# Now check permissions |
| 155 |
all_perms = get_perms(user, article) # ['delete_article'] |
| 156 |
direct_perms = get_user_perms(user, article) # Empty QuerySet |
| 157 |
group_perms = get_group_perms(user, article) # QuerySet with 'delete_article' |
| 158 |
``` |
| 159 |
|
| 160 |
### Mixed Permissions Example |
| 161 |
|
| 162 |
```python |
| 163 |
# User has both direct and group permissions |
| 164 |
assign_perm('change_article', user, article) # Direct permission |
| 165 |
assign_perm('delete_article', group, article) # Group permission (user is in group) |
| 166 |
|
| 167 |
all_perms = get_perms(user, article) # ['change_article', 'delete_article'] |
| 168 |
direct_perms = get_user_perms(user, article) # QuerySet with 'change_article' |
| 169 |
group_perms = get_group_perms(user, article) # QuerySet with 'delete_article' |
| 170 |
``` |