Forms
UserObjectPermissionsForm
Bases: BaseObjectPermissionsForm
Object level permissions management form for usage with User instances.
Attributes:
| Name | Type | Description |
|---|---|---|
user |
User
|
The user instance for which the permissions are being managed. |
Example
from django.shortcuts import get_object_or_404
from myapp.models import Post
from guardian.forms import UserObjectPermissionsForm
from django.contrib.auth.models import User
def my_view(request, post_slug, user_id):
user = get_object_or_404(User, id=user_id)
post = get_object_or_404(Post, slug=post_slug)
form = UserObjectPermissionsForm(user, post, request.POST or None)
if request.method == 'POST' and form.is_valid():
form.save_obj_perms()
...
Source code in guardian/forms.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
get_obj_perms_field_initial()
Returns initial object permissions management field choices.
Returns:
| Type | Description |
|---|---|
QuerySet[Permission]
|
List of permissions assigned to the user for the object. |
Source code in guardian/forms.py
143 144 145 146 147 148 149 | |
save_obj_perms()
Saves selected object permissions.
Saves selected object permissions by creating new ones and removing those which were not selected but already exists.
Should be called after form is validated.
Source code in guardian/forms.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
GroupObjectPermissionsForm
Bases: BaseObjectPermissionsForm
Object level permissions management form for usage with Group instances.
Attributes:
| Name | Type | Description |
|---|---|---|
group |
Group
|
The group instance for which the permissions are being managed. |
Example
from django.shortcuts import get_object_or_404
from myapp.models import Post
from guardian.forms import GroupObjectPermissionsForm
from guardian.models import Group
def my_view(request, post_slug, group_id):
group = get_object_or_404(Group, id=group_id)
post = get_object_or_404(Post, slug=post_slug)
form = GroupObjectPermissionsForm(group, post, request.POST or None)
if request.method == 'POST' and form.is_valid():
form.save_obj_perms()
...
Source code in guardian/forms.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
get_obj_perms_field_initial()
Returns initial object permissions management field choices.
Returns:
| Type | Description |
|---|---|
QuerySet[Permission]
|
List of permissions assigned to the group for the object. |
Source code in guardian/forms.py
198 199 200 201 202 203 204 | |
save_obj_perms()
Saves selected object permissions.
Saves selected object permissions by creating new ones and removing those which were not selected but already exists.
Should be called after form is validated.
Source code in guardian/forms.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |